Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/312.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 更新三维阵列时出现意外行为_Python_Iteration - Fatal编程技术网

Python 更新三维阵列时出现意外行为

Python 更新三维阵列时出现意外行为,python,iteration,Python,Iteration,事实上,问题是我试图获取一个单位细胞中原子位置的列表,并构造一个相应的超级细胞,给定在每个方向上重复细胞多少次的输入 这将产生一个循环结构: #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

事实上,问题是我试图获取一个单位细胞中原子位置的列表,并构造一个相应的超级细胞,给定在每个方向上重复细胞多少次的输入

这将产生一个循环结构:

#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]
的变化似乎比atom类型更频繁,而且由于索引似乎更新正确,我根本不理解这一点

为什么
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]
等等

编辑:如何初始化组件:

#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
编辑:组件[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]]

atomPositions[0][0]
将更改几次,这就是您的
for uuuuu-in-range(numEachAtom[l])
所做的

这个循环到底是为了什么?你就不能写吗

#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):
                atomPositions[l][index][0] = atomPos[l][index][0] + numEachAtom[l]*k
                atomPositions[l][index][1] = atomPos[l][index][1] + numEachAtom[l]*j
                atomPositions[l][index][2] = atomPos[l][index][2] + numEachAtom[l]*i
                print atomPositions[0][0]

编辑:op刚刚在循环中插入索引的地方更新了代码,所以这不再有意义了。

我正在使用
itertools
numpy
这两个非常有用的库,您应该熟悉它们。问题是:

取一个单位细胞中原子位置的列表,并构造一个相应的超级细胞,给定在每个方向上重复该细胞多少次的输入

以下是对任何维度的一般回答。为了便于说明,我使用了一个2D数组,但是如果你给它一个3D数组,它会工作得很好。使用
product
在所有可能的单元格方向上,您在帖子中使用的四个循环将替换为一个单循环。把它拆开玩,看看它是如何工作的

from numpy import *
from itertools import product

# Pass in the points, plus a vector that indicates the repeats in each direction
def supercell(R, v):
    v    = array(v) + 1
    n,d  = R.shape

    # Construct the copy directions
    CV  = list(product(*map(range,v)))
    R2  = zeros((len(CV)*n,d))

    for i,cell in enumerate(CV):
        R2[i*n:(i+1)*n,:] = cell+R
    return R2

# Construct some random points within a unit cell to work with
N,dimension = 100, 2
# Contrain them so you can see the supercell
R = random.random((N,dimension))*.6 + .5

R2 = supercell(R,[2,3])

# Plot the results
from pylab import *
scatter(*zip(*R2),color='b')
scatter(*zip(*R),color='r')
show()

在您的示例中,如果每个正方向上都有一个2的副本,那么向量
CV
将如下所示:

[(0, 0, 0), (0, 0, 1), (0, 0, 2), (0, 1, 0), (0, 1, 1), (0, 1, 2), (0, 2, 0), (0, 2, 1), (0, 2, 2), (1, 0, 0), (1, 0, 1), (1, 0, 2), (1, 1, 0), (1, 1, 1), (1, 1, 2), (1, 2, 0), (1, 2, 1), (1, 2, 2), (2, 0, 0), (2, 0, 1), (2, 0, 2), (2, 1, 0), (2, 1, 1), (2, 1, 2), (2, 2, 0), (2, 2, 1), (2, 2, 2)]

这就是
产品
功能的神奇之处。

它看起来很像是在某处乘以了输入列表。因为这是一个嵌套列表,所以它不会满足您的要求。

嘿,这是一个很好的问题!提供一个简短的输出示例可能会有帮助,这样人们就可以以一种具体的方式看到你的问题。你如何初始化<代码>原子< /代码>?你能为列表的内容提供一个工作示例吗?如果你在数组上做了很多数字的事情,考虑学习。“Marcin,我已经根据你的评论更新了这个问题。我不得不说,人们的回答速度之快给我留下了深刻的印象。我现在非常确定评论应该如何工作,所以再次请随意纠正我的任何滥用行为等。非常感谢,这应该是非常有用的。我以后一定会玩这个的。如果您有任何想法,我希望您能对问题中发布的代码为何表现得如此奇怪发表意见?@TCTopCat我一开始尝试过,但我不明白您为什么需要三个单独的索引。我看到的是从最里面开始的:(x,y,z)划分,然后是原子的指数。最外层的索引没有意义。在上面的例子中,我的超级细胞的形状是
(N,3)
,其中
N
是新的原子数。你的例子有
(x,N,3)
。什么是
x
?原子位置的第一个索引是指原子的类型。这有点复杂,但是为了我的输入文件(到一个叫做密度泛函理论的模拟软件包)的目的,我需要进行这种分离,以确保新的原子位置以相同元素的组写入文件。我不敢相信我没有看到这一点,但我找到了罪魁祸首。不过,值得注意的是,我发现这种行为相当违反直觉。谢谢大家花时间在这上面<代码>#扩展范围内i的额外原子数(len(atomPositions)):atomPositions[i]*=aNumber*bNumber*cNumber我假设在发布的代码之外一定发生了什么,因为其中没有任何内容会产生您报告的行为。