Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Replacing every 2nd element in the list

Tags:

python

I got 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 first one. So 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]]

Algorithm I use:

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 what I get is this:

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 are not changed, and it's because (I added index print to see that) it thinks that index of those elements are 7 and 9 for some reason. What can it be, because I am looking for a bug for so long still cannot find..

I double checked, there are not extra spaces or anything in the list.

like image 604
estranged Avatar asked Dec 13 '14 17:12

estranged


People also ask

How to replace elements in second list with index of same element?

Python | Replace elements in second list with index of same element in first list. Given two lists of strings, where first list contains all elements of second list, the task is to replace every element in second list with index of elements in first list. Method #1: Using Iteration. # Python code to replace every element.

How to replace item in a list in Python?

Here, we shall be looking into 7 different ways in order to replace item in a list in python. 1. Using list indexing The list elements can be easily accessed with the help of indexing. This is the most basic and the easiest method of accessing list elements.

How to replace the first item in a list using indexing?

The first element is stored at index 0, and the last element is stored at index ‘len (list)-1’. Let us take a list named ‘my_list’, which stores names of colors. Now, if we want to replace the first item inside the list from ‘Red’ to ‘Black’, we can do that using indexing. We assign the element stored at the 0th index to a new value.

How to return every second element of a list in Python?

To return every second element we have to change the start value to 1. Now, create a function that uses a for loop and the range function to generate list indexes. The first argument (start) is equal to 1 and the second argument (end) is equal to the length of the list.


1 Answers

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

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

Or use repeat from itertools (unfortunately this is twice slower for small array):

from itertools import repeat

for l in tempL: 
    l[::2] = repeat(0,(len(l)+1)/2)
like image 62
Arpegius Avatar answered Oct 19 '22 09:10

Arpegius