维度与nd数组不匹配

时间:2016-06-28 08:13:03

标签: python

我已经从文件中创建了一个nd数组:

for i in range(int(atoms)):
    next(finp)
    for j in range(int(ldata[2])):
        aatom[i][j] = [float(x) for x in
                       finp.readline().strip().split()]

我期待成为一个3d数组(i,j,x)。但事实并非如此。将它转换为numpy数组后:

atom = np.array(aatom)
print(atom.shape)

yeilds:(250, 301)这是i,j的维度。但是,在我的思考过程中,确实是3d:

print(atom[1][1][:])

yeilds: [-3.995, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0]

我在这里做错了什么?

1 个答案:

答案 0 :(得分:1)

你的嵌套列表可能有不同的长度,迫使NumPy创建一个dtype=object的数组,这意味着一个list而不是float的数组。

比较

a = [range(3), range(2)]  # different lenghts in inner lists
aa = np.array(a)
print aa
print aa.shape            # (2,)
print aa.dtype            # dtype('O')

b = [range(3), range(3)]  # equal lenghts in inner lists
bb = np.array(b)
print bb
print bb.shape            # (2, 3)
print bb.dtype            # dtype('int64')
相关问题