How to change items in a list in a list

I am very new to Python trying to learn the basics. Do not doubt this list.

You have a list:

L = [[1,2,3],[4,5,6],[3,4,6]] 

The output should be:

 [[2,4,6],[8,10,12],[6,8,12]] 

The code that works for me is as follows

 for x in range(len(L)): for y in range(len(L[x])): L[x][y] = L[x][y] + L[x][y] print L 

It displays [[2,4,6],[8,10,12],[6,8,12]] .

Now I want to get the same result with a different code:

 for x in L: a = L.index(x) for y in L[a]: b = L[a].index(y) L[a][b] = L[a][b] + L[a][b] print L 

With the above code, the result is:

 [[4,2,6],[8,10,12],[12,8,6]] 

I tried to debug this output. I put an inscription above the line "L[a][b] = L[a][b] + L[a][b]" to print a and b. I was surprised to see the values โ€‹โ€‹of a and b:

 0,0 0,0 0,2 1,0 1,1 1,2 2,0 2,1 2,0 

Again, if I comment on the line "L[a][b] = L[a][b] + L[a][b]" , then the values โ€‹โ€‹of a and b will be as expected:

 0,0 0,1 0,2 1,0 1,1 1,2 2,0 2,1 2,2 

I suspect this may be due to the volume of variables in python and tried to study some material on scope in python. But I did not receive a corresponding answer either for the review or for the above question.

+5
source share
5 answers

As other people explained, when you use the index function, it finds the first occurrence of the value you are looking for. So, the first time through the loop (for the first line) it looks like

 b = 1 [1,2,3].find(1) # returns index 0 #Then you modify the first element of the list b = 2 [2,2,3].find(2) #returns index 0 again! 

To get indexes in a simpler and more deterministic way, you can use the enumerate function in a list. It will provide you with an iterator that returns the value of the AND index as you move through the entire list.

 for rowInd, x in enumerate(L): for colInd, y in enumerate(x): L[rowInd][colInd] = y + y 

Note that this will do this locally, as in your original solution.

L = [[2, 4, 6], [8, 10, 12], [6, 8, 12]]

0
source

You change your list using the operator - L[a][b] = L[a][b] + L[a][b] for example -

 L = [[1, 2, 3], [4, 5, 6], [3, 4, 6]] 

L[0][0] = 1 initially Then you change it as L [0] [0] = 2

 L = [[2, 2, 3], [4, 5, 6], [3, 4, 6]] 

In the next cycle, you index search index 2, which is now 0.0, because you changed the list L. I tried to print L along with a, b in your example. The result explains the behavior -

 0 0 [[1, 2, 3], [4, 5, 6], [3, 4, 6]] 0 0 [[2, 2, 3], [4, 5, 6], [3, 4, 6]] 0 2 [[4, 2, 3], [4, 5, 6], [3, 4, 6]] 1 0 [[4, 2, 6], [4, 5, 6], [3, 4, 6]] 1 1 [[4, 2, 6], [8, 5, 6], [3, 4, 6]] 1 2 [[4, 2, 6], [8, 10, 6], [3, 4, 6]] 2 0 [[4, 2, 6], [8, 10, 12], [3, 4, 6]] 2 1 [[4, 2, 6], [8, 10, 12], [6, 4, 6]] 2 0 [[4, 2, 6], [8, 10, 12], [6, 8, 6]] 
+1
source

The best way to achieve the desired result is to use list comprehension . You can do the following:

 L = [[1,2,3], [4,5,6], [3,4,6]] answer = [[2*el for el in sublist] for sublist in L] print(answer) 

Output

 [[2, 4, 6], [8, 10, 12], [6, 8, 12]] 

This is repeated through each sublist in your L list and multiplies each el by sublist by 2, thereby achieving the desired result.

0
source

I think the following code snippet might be better for x in L: #iterating over the orig list for y in x: # all over the internal list [x] [y] = [x] [y] + [x] [y ]

0
source

If you insist on using the second method, you need to save the results in a temporary variable:

 L = [[1, 2, 3], [4, 5, 6], [3, 4, 6]] M = [[0 for y in range(3)] for x in range(3)] for x in L: a = L.index(x) for y in L[a]: b = L[a].index(y) M[a][b] = L[a][b] + L[a][b] L = M print L 

Output:

 [[2, 4, 6], [8, 10, 12], [6, 8, 12]] 
0
source

Source: https://habr.com/ru/post/1243876/


All Articles