gpt4 book ai didi

python - 更新 3D 数组时的意外行为

转载 作者:行者123 更新时间:2023-11-28 17:51:07 26 4
gpt4 key购买 nike

实际上,问题是我试图获取一个单元格中原子位置的列表并构建一个相应的 super 单元格,给出在每个方向上重复单元格的次数的输入。

这导致了一个循环结构:

#Create Every Unit Cell in SuperCell
aNum=2
bNum=2
cNum=2
atomPos = copy.deepcopy(atomPositions)
for l in range(len(atomPos)):
index=0
for i in range(cNum):
for j in range(bNum):
for k in range(aNum):
for _ in range(numEachAtom[l]):
atomPositions[l][index][0] = atomPos[l][index][0] + 1*k
atomPositions[l][index][1] = atomPos[l][index][1] + 1*j
atomPositions[l][index][2] = atomPos[l][index][2] + 1*i
print atomPositions[0][0]
index += 1

其中 atomPositions 是一个 3D 数组,使得:atomPositions[atomtype=l][atom=index][atomposition=0] = [x,y,z] 打印语句用于诊断目的。

问题是,从 print 语句来看,atomPositions[0][0] 的变化似乎比原子类型更频繁,而且索引似乎正在正确更新,我根本不明白这。

为什么 atomPositions[0][0]l 更频繁地变化?

最初我遇到了修改您正在迭代的列表的问题,因此一开始就是深拷贝。任何意见将不胜感激!

附言这是我第一次提问,我是 Python 的初学者,所以请随时评论我缺乏格式/清晰度/风格等问题。

编辑:numEachAtom=[4,6] 的输出开头示例:

[0.17611251675504244, 0.17611251675504244, 0.17611251675504244]
[0.17611251675504244, 0.17611251675504244, 0.17611251675504244]
[0.17611251675504244, 0.17611251675504244, 0.17611251675504244]
[0.17611251675504244, 0.17611251675504244, 0.17611251675504244]
[1.1761125167550424, 0.17611251675504244, 0.17611251675504244]
[1.1761125167550424, 0.17611251675504244, 0.17611251675504244]
[1.1761125167550424, 0.17611251675504244, 0.17611251675504244]
[1.1761125167550424, 0.17611251675504244, 0.17611251675504244]
[0.17611251675504244, 1.1761125167550424, 0.17611251675504244]
[0.17611251675504244, 1.1761125167550424, 0.17611251675504244]
[0.17611251675504244, 1.1761125167550424, 0.17611251675504244]

等等

编辑: atomPositions 是如何初始化的:

#Separate out positions of different atom types
atomPositions = []
counter=0
for i,atomN in enumerate(numEachAtom):
atomPositions.append( origAtomPositions[counter:counter+atomN] )
counter += atomN

编辑:依赖等

import sys
import scipy as sp
from scipy import *
import copy

编辑 atomPositions[0] 初始化为

[[ 0.17611252  0.17611252  0.17611252]
[ 0.32388748 0.32388748 0.32388748]
[ 0.42611252 0.42611252 0.42611252]
[ 0.07388748 0.07388748 0.07388748]
[ 0.17611252 0.17611252 0.17611252]
[ 0.32388748 0.32388748 0.32388748]
[ 0.42611252 0.42611252 0.42611252]
[ 0.07388748 0.07388748 0.07388748]
[ 0.17611252 0.17611252 0.17611252]
[ 0.32388748 0.32388748 0.32388748]
[ 0.42611252 0.42611252 0.42611252]
[ 0.07388748 0.07388748 0.07388748]
[ 0.17611252 0.17611252 0.17611252]
[ 0.32388748 0.32388748 0.32388748]
[ 0.42611252 0.42611252 0.42611252]
[ 0.07388748 0.07388748 0.07388748]
[ 0.17611252 0.17611252 0.17611252]
[ 0.32388748 0.32388748 0.32388748]
[ 0.42611252 0.42611252 0.42611252]
[ 0.07388748 0.07388748 0.07388748]
[ 0.17611252 0.17611252 0.17611252]
[ 0.32388748 0.32388748 0.32388748]
[ 0.42611252 0.42611252 0.42611252]
[ 0.07388748 0.07388748 0.07388748]
[ 0.17611252 0.17611252 0.17611252]
[ 0.32388748 0.32388748 0.32388748]
[ 0.42611252 0.42611252 0.42611252]
[ 0.07388748 0.07388748 0.07388748]
[ 0.17611252 0.17611252 0.17611252]
[ 0.32388748 0.32388748 0.32388748]
[ 0.42611252 0.42611252 0.42611252]
[ 0.07388748 0.07388748 0.07388748]]

最佳答案

它看起来非常像是在某个地方增加了输入列表。由于这是一个嵌套列表,因此它不会执行您想要的操作。

关于python - 更新 3D 数组时的意外行为,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9682613/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com