1我目前正在使用结构化数组来保存传感器的一些测量值。数组(名为“data”)的维度为 2000x3,包含三个字段:“samples”、“timestamp”和“labels”,其中 samples 是 6 个元素的向量。例如,一行如下所示:
([-19.837763650963275, -19.61692779005053, -18.5301618270122, -13.413484985874076, -13.192649124961326, -12.105883161923], 0.0, 0)
[('samples', '<f8', (6,)), ('timestamp', '<f8'), ('labels', '<i4')]
如果我现在想拥有一行的所有样本,我可以像这样访问该行:
data["samples"][10]
效果很好,但如果我把它转过来写成这样:
data[10]["samples"]
我收到以下错误:
ValueError:具有多个元素的数组的真值不明确。使用 a.any() 或 a.all()
有谁知道为什么会这样?
编辑:为了更好地理解这里是“数据”数组的前 10 行:
[ ([-19.837763650963275, -19.61692779005053, -18.5301618270122, -13.413484985874076, -13.192649124961326, -12.105883161923], 0.0, 0) ([-18.66282477705446, -18.449317421024432, -17.369283339067675, -12.357118609142269, -12.145937637117552, -11.0680867207074204], 0.0, ([-17.69388207920866, -17.49198382816449, -16.417085567075173, -11.53193799727324, -11.33976221074188, -10.268890664091229], 0.0, 0) ([-16.868088042481606, -16.683019283158636, -15.610803357043569, -10.88697503359368, -10.732729221349611, -9.663174869208511], 0.0, ([-16.152597338007514, -15.99074244228478, -14.917542852993487, -10.420257243109129, -10.371835643625495, -9.274470774420056], 0.0, ([-15.527885804583931, -15.39736918876727, -14.317660328685264, -10.451759205557037, -10.011944429521288, -9.006031495483906], 0.0, ([-14.981993405744573, -14.894234811642814, -13.799157258392123, -10.01393043959338, -9.563854671143899, -8.587392657229502], 0.0, 0) ([-14.508475562047407, -14.48315918653869, -13.355984261155621, -9.577456476813333, -9.16920566037696, -8.205174149255923], 0.0, 0) ([-14.106856958780497, -14.199089434545343, -12.989626950396643, -9.214711746255777, -8.944244361010687, -7.942834820090279], 0.0, 0) ([-13.789298779585817, -13.943732248940886, -12.72321280228509, -9.208476598556874, -8.629970466866272, -7.690122078593869], 0.0, 0)
编辑 2:我使用的是 numpy 版本 1.10.4
我实际上并没有使用
data[0]["samples"]
但我试过了,它也不起作用。我这样使用它:
对于数据行: 打印(行[“样本”])
我做了一个我能想到的最基本的例子,它的效果令人惊讶:
我做了一个我能想到的最基本的例子,它的效果令人惊讶:
a = np.zeros(10, dtype = [("a", "5f8"), ("b", "i4")])
print(a["a"][5]) # Works
print(a[5]["a"]) # Surprisingly works as well