python: FOR 循环,迭代赋值未分配正确的值

python: FOR loop, Iterating assignment not assigning correct value

我正在制作一个列表,其中嵌套了一个列表。我需要遍历第二个列表,并为位置分配一个值。所以我写了这个

matrix=[]
list=[0,1,2,3,4,5,6,7,8,9]
def board():
    for x in range(10):
        for y in range(10):
            matrix.append([[x,y],1]) #A grid is made, with a spare element
    for i in range(100):
        matrix[i][1]=list            #The grid is two elements, co-ords...
                                     #and a list of attributes


board()
for i in range(100):
    matrix[i][2][0]=i        #The first attribute,(in 2nd of2elements),
print(matrix)                ##SHOULD be its own position

此代码的所有变体都会产生相同的结果:

Error or the number "99" being placed in EVERY INSTANCE of that location. That is to say my final FOR loop seems to be assigning "99" NOT i...

我已经尝试了几个变体(在 FOR 循环内生成数字是我的偏好,唉)我花了大约 6 个小时盯着这个看。 我所有的逻辑都告诉我,计算机应该知道我想要第二个元素的第一个元素是计数 0,1,2... 我知道这不可能是因为语言限制,所以请告诉我我没有看到的内容。

matrix[i][1]=list

执行此操作时,相同的数组被分配到矩阵数组中。因此,如果您更改一项,则所有项都会更改。而是像这样分配列表的副本:

matrix[i][1] = list[:]

正如@Bakuriu 所指出的,在 python 3 中你可以使用 list.copy() 方法。在 Python 2 中,您可以使用 copy 模块来复制带有 l2 = copy.copy(l1)

的容器

此外,您应该更改变量 'list' 的名称,因为 'list' 是 python 中的内置类型。

您应该替换 matrix[i][2][0]=i 赋值,因为它会引发 IndexError 到:

for i in range(100):
    matrix[i][1][0]=i        #The first attribute,(in 2nd of2elements),
print(matrix)                ##SHOULD be its own position

列表在 for 循环中更快,但这里你有字典的解决方案:

from pprint import pprint as ppr

matrix={}
list=[0,1,2,3,4,5,6,7,8,9]
def board():
    for x in range(10):
        for y in range(10):
            matrix[(x,y)]=1 
    for k in matrix.keys():
        matrix[k]=list

board()
ppr(matrix)

for k in matrix.iterkeys():
    v=matrix[k][:]   
    v[2]=k[0]*10+k[1]
    matrix[k]=v
ppr(matrix)     

输入:

{(0, 0): [0, 1, 2, 3, 4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0, 1, 8, 3, 4, 5, 6, 7, 8, 9],
 (0, 9): [0, 1, 9, 3, 4, 5, 6, 7, 8, 9],
 (1, 0): [0, 1, 10, 3, 4, 5, 6, 7, 8, 9],
 (1, 1): [0, 1, 11, 3, 4, 5, 6, 7, 8, 9],
 (1, 2): [0, 1, 12, 3, 4, 5, 6, 7, 8, 9],
 (1, 3): [0, 1, 13, 3, 4, 5, 6, 7, 8, 9],
 (1, 4): [0, 1, 14, 3, 4, 5, 6, 7, 8, 9],
 (1, 5): [0, 1, 15, 3, 4, 5, 6, 7, 8, 9],
 (1, 6): [0, 1, 16, 3, 4, 5, 6, 7, 8, 9],
 (1, 7): [0, 1, 17, 3, 4, 5, 6, 7, 8, 9],
 (1, 8): [0, 1, 18, 3, 4, 5, 6, 7, 8, 9],
 (1, 9): [0, 1, 19, 3, 4, 5, 6, 7, 8, 9],
 (2, 0): [0, 1, 20, 3, 4, 5, 6, 7, 8, 9],
 (2, 1): [0, 1, 21, 3, 4, 5, 6, 7, 8, 9],
 (2, 2): [0, 1, 22, 3, 4, 5, 6, 7, 8, 9],
 (2, 3): [0, 1, 23, 3, 4, 5, 6, 7, 8, 9],
 (2, 4): [0, 1, 24, 3, 4, 5, 6, 7, 8, 9],
 (2, 5): [0, 1, 25, 3, 4, 5, 6, 7, 8, 9],
 (2, 6): [0, 1, 26, 3, 4, 5, 6, 7, 8, 9],
 (2, 7): [0, 1, 27, 3, 4, 5, 6, 7, 8, 9],
 (2, 8): [0, 1, 28, 3, 4, 5, 6, 7, 8, 9],
 (2, 9): [0, 1, 29, 3, 4, 5, 6, 7, 8, 9],
 (3, 0): [0, 1, 30, 3, 4, 5, 6, 7, 8, 9],
 (3, 1): [0, 1, 31, 3, 4, 5, 6, 7, 8, 9],
 (3, 2): [0, 1, 32, 3, 4, 5, 6, 7, 8, 9],
 (3, 3): [0, 1, 33, 3, 4, 5, 6, 7, 8, 9],
 (3, 4): [0, 1, 34, 3, 4, 5, 6, 7, 8, 9],
 (3, 5): [0, 1, 35, 3, 4, 5, 6, 7, 8, 9],
 (3, 6): [0, 1, 36, 3, 4, 5, 6, 7, 8, 9],
 (3, 7): [0, 1, 37, 3, 4, 5, 6, 7, 8, 9],
 (3, 8): [0, 1, 38, 3, 4, 5, 6, 7, 8, 9],
 (3, 9): [0, 1, 39, 3, 4, 5, 6, 7, 8, 9],
 (4, 0): [0, 1, 40, 3, 4, 5, 6, 7, 8, 9],
 (4, 1): [0, 1, 41, 3, 4, 5, 6, 7, 8, 9],
 (4, 2): [0, 1, 42, 3, 4, 5, 6, 7, 8, 9],
 (4, 3): [0, 1, 43, 3, 4, 5, 6, 7, 8, 9],
 (4, 4): [0, 1, 44, 3, 4, 5, 6, 7, 8, 9],
 (4, 5): [0, 1, 45, 3, 4, 5, 6, 7, 8, 9],
 (4, 6): [0, 1, 46, 3, 4, 5, 6, 7, 8, 9],
 (4, 7): [0, 1, 47, 3, 4, 5, 6, 7, 8, 9],
 (4, 8): [0, 1, 48, 3, 4, 5, 6, 7, 8, 9],
 (4, 9): [0, 1, 49, 3, 4, 5, 6, 7, 8, 9],
 (5, 0): [0, 1, 50, 3, 4, 5, 6, 7, 8, 9],
 (5, 1): [0, 1, 51, 3, 4, 5, 6, 7, 8, 9],
 (5, 2): [0, 1, 52, 3, 4, 5, 6, 7, 8, 9],
 (5, 3): [0, 1, 53, 3, 4, 5, 6, 7, 8, 9],
 (5, 4): [0, 1, 54, 3, 4, 5, 6, 7, 8, 9],
 (5, 5): [0, 1, 55, 3, 4, 5, 6, 7, 8, 9],
 (5, 6): [0, 1, 56, 3, 4, 5, 6, 7, 8, 9],
 (5, 7): [0, 1, 57, 3, 4, 5, 6, 7, 8, 9],
 (5, 8): [0, 1, 58, 3, 4, 5, 6, 7, 8, 9],
 (5, 9): [0, 1, 59, 3, 4, 5, 6, 7, 8, 9],
 (6, 0): [0, 1, 60, 3, 4, 5, 6, 7, 8, 9],
 (6, 1): [0, 1, 61, 3, 4, 5, 6, 7, 8, 9],
 (6, 2): [0, 1, 62, 3, 4, 5, 6, 7, 8, 9],
 (6, 3): [0, 1, 63, 3, 4, 5, 6, 7, 8, 9],
 (6, 4): [0, 1, 64, 3, 4, 5, 6, 7, 8, 9],
 (6, 5): [0, 1, 65, 3, 4, 5, 6, 7, 8, 9],
 (6, 6): [0, 1, 66, 3, 4, 5, 6, 7, 8, 9],
 (6, 7): [0, 1, 67, 3, 4, 5, 6, 7, 8, 9],
 (6, 8): [0, 1, 68, 3, 4, 5, 6, 7, 8, 9],
 (6, 9): [0, 1, 69, 3, 4, 5, 6, 7, 8, 9],
 (7, 0): [0, 1, 70, 3, 4, 5, 6, 7, 8, 9],
 (7, 1): [0, 1, 71, 3, 4, 5, 6, 7, 8, 9],
 (7, 2): [0, 1, 72, 3, 4, 5, 6, 7, 8, 9],
 (7, 3): [0, 1, 73, 3, 4, 5, 6, 7, 8, 9],
 (7, 4): [0, 1, 74, 3, 4, 5, 6, 7, 8, 9],
 (7, 5): [0, 1, 75, 3, 4, 5, 6, 7, 8, 9],
 (7, 6): [0, 1, 76, 3, 4, 5, 6, 7, 8, 9],
 (7, 7): [0, 1, 77, 3, 4, 5, 6, 7, 8, 9],
 (7, 8): [0, 1, 78, 3, 4, 5, 6, 7, 8, 9],
 (7, 9): [0, 1, 79, 3, 4, 5, 6, 7, 8, 9],
 (8, 0): [0, 1, 80, 3, 4, 5, 6, 7, 8, 9],
 (8, 1): [0, 1, 81, 3, 4, 5, 6, 7, 8, 9],
 (8, 2): [0, 1, 82, 3, 4, 5, 6, 7, 8, 9],
 (8, 3): [0, 1, 83, 3, 4, 5, 6, 7, 8, 9],
 (8, 4): [0, 1, 84, 3, 4, 5, 6, 7, 8, 9],
 (8, 5): [0, 1, 85, 3, 4, 5, 6, 7, 8, 9],
 (8, 6): [0, 1, 86, 3, 4, 5, 6, 7, 8, 9],
 (8, 7): [0, 1, 87, 3, 4, 5, 6, 7, 8, 9],
 (8, 8): [0, 1, 88, 3, 4, 5, 6, 7, 8, 9],
 (8, 9): [0, 1, 89, 3, 4, 5, 6, 7, 8, 9],
 (9, 0): [0, 1, 90, 3, 4, 5, 6, 7, 8, 9],
 (9, 1): [0, 1, 91, 3, 4, 5, 6, 7, 8, 9],
 (9, 2): [0, 1, 92, 3, 4, 5, 6, 7, 8, 9],
 (9, 3): [0, 1, 93, 3, 4, 5, 6, 7, 8, 9],
 (9, 4): [0, 1, 94, 3, 4, 5, 6, 7, 8, 9],
 (9, 5): [0, 1, 95, 3, 4, 5, 6, 7, 8, 9],
 (9, 6): [0, 1, 96, 3, 4, 5, 6, 7, 8, 9],
 (9, 7): [0, 1, 97, 3, 4, 5, 6, 7, 8, 9],
 (9, 8): [0, 1, 98, 3, 4, 5, 6, 7, 8, 9],
 (9, 9): [0, 1, 99, 3, 4, 5, 6, 7, 8, 9]}

你可以在这里使用 numpy,快速索引(更快的是 cython memoryview 所以也看看它):

matrix=np.zeros((100,10),dtype=int)
list=[0,1,2,3,4,5,6,7,8,9]
def board(matrix):
    for x in range(10):
        for y in range(10):
            matrix[x*10+y]=1 #but here You populate whole sub-array of shape (10) with 1, so it will be: [1,1,1,1...,1] 
    print matrix
    for k in range(matrix.shape[0]):
        matrix[k]=list

board(matrix)
ppr(matrix)

for k in range(matrix.shape[0]):
    matrix[k,2]=k
ppr(matrix)