Replacing every second item in a list

I have a 2 dimensional list:

[[5, 80, 2, 57, 5, 97], [2, 78, 2, 56, 6, 62], [5, 34, 3, 54, 6, 5, 2, 58, 5, 61, 5, 16]] 

In which I need to change every second element to 0, starting from the first. Therefore, it should look like this:

 [[0, 80, 0, 57, 0, 97], [0, 78, 0, 56, 0, 62], [0, 34, 0, 54, 0, 5, 0, 58, 0, 61, 0, 16]] 

The algorithm I use is:

 for i in tempL: for j, item in enumerate(i): if i.index(item) % 2 == 0: print('change, index:'), print(i.index(item)) i[j] = 0 else: print('not change, index:'), print(i.index(item)) 

But I get the following:

 change, index: 0 not change, index: 1 change, index: 2 not change, index: 3 change, index: 4 not change, index: 5 change, index: 0 not change, index: 1 change, index: 2 not change, index: 3 change, index: 4 not change, index: 5 change, index: 0 not change, index: 1 change, index: 2 not change, index: 3 change, index: 4 not change, index: 5 change, index: 6 not change, index: 7 not change, index: 5 not change, index: 9 not change, index: 5 not change, index: 11 [[0, 80, 0, 57, 0, 97], [0, 78, 0, 56, 0, 62], [0, 34, 0, 54, 0, 5, 0, 58, 5, 61, 5, 16]] 

Some elements do not change, and because (I added an index print to see this), he believes that the index of these elements for some reason is 7 and 9. What could be because I searched for the error for so long, until I can to find.

I double-checked that there were no extra spaces or anything in the list.

+5
source share
4 answers

I think your algorithm is correct. You just made a logical mistake. Using i.index , each time you look for an internal list for this value. It is not only expensive, but also sensitive to duplicate values.

 for i in tempL: for j, item in enumerate(i): # if i.index(item) % 2 == 0: oops if j % 2 == 0: print('change, index:'), print(i.index(item)) i[j] = 0 else: print('not change, index:'), print(i.index(item)) 
+2
source

Well, this task should be obvious. Use the slice assignment! You need to assign an array of zeros that are half the length. To create one, simply multiply one elemental array with the value:

 for l in tempL: l[::2] = [0] * ((len(l)+1)/2) 

Or use repeat from itertools (unfortunately this is twice as slow for a small array):

 from itertools import repeat for l in tempL: l[::2] = repeat(0,(len(l)+1)/2) 
+3
source

As a more pythonic way, you can simply use list comprehension:

 >>> l=[[5, 80, 2, 57, 5, 97], [2, 78, 2, 56, 6, 62], [5, 34, 3, 54, 6, 5, 2, 58, 5, 61, 5, 16]] >>> l=[[t if k%2 else 0 for k,t in enumerate(i)] for i in l] >>> l [[0, 80, 0, 57, 0, 97], [0, 78, 0, 56, 0, 62], [0, 34, 0, 54, 0, 5, 0, 58, 0, 61, 0, 16]] 
+1
source

Just add two cents:

Using the Modulo Operator

 mylist= [[5, 80, 2, 57, 5, 97], [2, 78, 2, 56, 6, 62], [5, 34, 3, 54, 6, 5, 2, 58, 5, 61, 5, 16]] for i,n in enumerate(mylist): for j,m in enumerate(n): if j % 2 == 0: mylist[i][j] = 0 print mylist 

Conclusion:

 [[0, 80, 0, 57, 0, 97], [0, 78, 0, 56, 0, 62], [0, 34, 0, 54, 0, 5, 0, 58, 0, 61, 0, 16]] 
0
source

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


All Articles