我试图构造一个numpy数组,然后将整数和另一个数组附加到它。 我试过这样做:
xyz_list = frag_str.split()
nums = numpy.array([])
coords = numpy.array([])
for i in range(int(len(xyz_list)/4)):
numpy.append(nums, xyz_list[i*4])
numpy.append(coords, xyz_list[i*4+1:(i+1)*4])
print(atoms)
print(coords)
打印输出只会得到空数组。为什么?
另外,我如何重写coords
以允许我有这样的二维数组:array[[0,0,0],[0,0,1],[0,0,-1]]
?
与python的
list.append
不同,numpy.append
不执行适当的操作。因此,您需要将结果分配回变量,如下所示。您可以按如下方式重塑
coords
:有关
numpy.append
行为的更多详细信息Documentation:
如果您事先知道
numpy
数组输出的形状,那么通过np.zeros(n)
实例化并在稍后用结果填充它是有效的。另一种选择是:如果您的计算大量使用在数组的左侧插入元素,请考虑使用标准库中的^{} 。
如上所述,
numpy.append
不在适当位置追加项,但重要的原因是。必须将返回的数组从numpy.append
存储到原始变量,否则代码将无法工作。尽管如此,你应该重新思考你的逻辑。Numpy在内部使用C样式的数组,这些数组位于连续内存中,没有前导或尾随未使用的元素。为了将一个项追加到一个数组中,Numpy必须分配一个数组大小为+1的缓冲区,复制所有数据并添加追加的元素。
在pseudo-C代码中,会出现以下情况:
这是非常低效的,因为每次都必须分配一个新数组(内存分配很慢),必须复制所有元素,并将新元素添加到新数组的末尾。
相比之下,Python列表会保留超出容器大小的额外元素,直到其大小与列表的容量相同,并呈指数增长。对于容器末尾的插入,这比每次重新分配整个缓冲区要高效得多。
您应该使用Python列表和
list.append
,然后将新列表转换为NumPy数组。或者,如果性能非常关键,在所有场景中使用^ {CD4}}而不是^ {CD1}}使用C++扩展。重新编写你的代码,否则它将是冰封的。编辑
此外,正如注释中指出的,如果您事先知道Numpy数组的大小,那么使用
np.zeros(n)
预分配它是有效的,就像在Numpy数组周围使用自定义包装一样np.append
不是列表克隆。这是一个笨拙的包装。最好学会正确使用它。List append更快,更容易初始化。}。
np.concatenate
可以很好地处理数组列表。np.append
使用concatenate
,但只接受两个输入。^如果列表包含数字或字符串,则需要{你没有给出
frag_str
的例子。但是split
的名称和用法表明它是一个字符串。我认为其他任何东西都没有split
方法。这是一个字符串列表。使用索引,我可以构造两个列表:
我可以从这些列表中创建数组:
在第一种情况下,数组是1d,在第二种情况下,数组是2d
如果字符串包含数字,我们可以通过指定
dtype
来生成整数数组。相关问题 更多 >
编程相关推荐