1

I am making a list, with a list nested inside. This second list I need to iterate through, and assign a value to a position. So I wrote this

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

All variations of this code result in the same thing:

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...

I have tried several variations of this (generating the numbers inside the FOR loop was my preference, alas) I have spent about 6 hours staring at this. All my logic tells me that the computer should KNOW that I want the first element, of that second element, to be a count 0,1,2... I know this can't be because of a limit on the language, so tell me what I am not seeing.

3
  • 1
    Do not use built-in names as variable names. I suggest you use a name different from list for your variable, like numbers = [...]. Commented May 2, 2016 at 13:06
  • You could use an if statement in the x y loop to assign list to a matrix element couldn't you? Commented May 2, 2016 at 13:07
  • Plus, matrix[i][2][0]=i raises an IndexError as your matrix is filled by lists of two elements... Commented May 2, 2016 at 13:13

3 Answers 3

2

matrix[i][1]=list

When you do this the same array is being assigned in the matrix array. Hence if you change one item, all the items get changed. Instead assign a copy of the list like this:

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

As pointed out by @Bakuriu , in python 3 you can use list.copy() method. In Python 2 you can use the copy module to copy containers with l2 = copy.copy(l1)

Also, you should change the name of the variable 'list' as 'list' is an inbuilt type in python.

Sign up to request clarification or add additional context in comments.

3 Comments

Note: in python3 all containers have a copy method so you could write: matrix[i][1] = list.copy(). this is, IMO, clearer than the [:] syntax.
OOOH, ok so if i understand: that particular item in the list was being updated to be the same as every other time i updated that item in another location. I knew this happened with variables, so that the original variable changed like... a=6...b=a.....b=7 and then a suddenly equals 7...(I cant imagine when that would be useful(yet))
Thanks to everyone who helped, and pointed that one style gaffe
0

You should replace matrix[i][2][0]=i assignement as it raises an IndexError to:

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

1 Comment

Quite right. I had left that in from another version attempt wherein the list WAS the third item i.e. matrix =[x,y,list]
0

Lists are faster in for loop, but here You have solution with dicts:

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)     

Input:

{(0, 0): [0, 1, 2, 3, 4, 5, 6, 7, 8, 9],
 (0, 1): [0, 1, 2, 3, 4, 5, 6, 7, 8, 9],
 (0, 2): [0, 1, 2, 3, 4, 5, 6, 7, 8, 9],
 (0, 3): [0, 1, 2, 3, 4, 5, 6, 7, 8, 9],
 (0, 4): [0, 1, 2, 3, 4, 5, 6, 7, 8, 9],
 (0, 5): [0, 1, 2, 3, 4, 5, 6, 7, 8, 9],
 (0, 6): [0, 1, 2, 3, 4, 5, 6, 7, 8, 9],
 (0, 7): [0, 1, 2, 3, 4, 5, 6, 7, 8, 9],
 (0, 8): [0, 1, 2, 3, 4, 5, 6, 7, 8, 9],
 (0, 9): [0, 1, 2, 3, 4, 5, 6, 7, 8, 9],
 (1, 0): [0, 1, 2, 3, 4, 5, 6, 7, 8, 9],
 (1, 1): [0, 1, 2, 3, 4, 5, 6, 7, 8, 9],
 (1, 2): [0, 1, 2, 3, 4, 5, 6, 7, 8, 9],
 (1, 3): [0, 1, 2, 3, 4, 5, 6, 7, 8, 9],
 (1, 4): [0, 1, 2, 3, 4, 5, 6, 7, 8, 9],
 (1, 5): [0, 1, 2, 3, 4, 5, 6, 7, 8, 9],
 (1, 6): [0, 1, 2, 3, 4, 5, 6, 7, 8, 9],
 (1, 7): [0, 1, 2, 3, 4, 5, 6, 7, 8, 9],
 (1, 8): [0, 1, 2, 3, 4, 5, 6, 7, 8, 9],
 (1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9]}

Output:

{(0, 0): [0, 1, 0, 3, 4, 5, 6, 7, 8, 9],
 (0, 1): [0, 1, 1, 3, 4, 5, 6, 7, 8, 9],
 (0, 2): [0, 1, 2, 3, 4, 5, 6, 7, 8, 9],
 (0, 3): [0, 1, 3, 3, 4, 5, 6, 7, 8, 9],
 (0, 4): [0, 1, 4, 3, 4, 5, 6, 7, 8, 9],
 (0, 5): [0, 1, 5, 3, 4, 5, 6, 7, 8, 9],
 (0, 6): [0, 1, 6, 3, 4, 5, 6, 7, 8, 9],
 (0, 7): [0, 1, 7, 3, 4, 5, 6, 7, 8, 9],
 (0, 8): [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]}

You could use numpy here, with fast indexing (faster is cython memoryview so look at it too):

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)     

1 Comment

I trust that this is useful too, but I haven't worked with dicts so I will use your example to experiment with them, to learn them. Thanks for it :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.