简体   繁体   中英

Replace value of a list in python

What I want to do is replace all the even numbers of a list with 0 for instance

list = [1,2,3,4,5] would be list = [1,0,3,0,5]

I thought about doing it this way

list = [1,2,3,4,5]

for i in list:
  if i % 2 == 0:
  # then replace the even numbers with 0 

the problem is that I cant figure out how to write the next line of code

The problem is that if you start like this

for i in lst: 
    if i %2 == 0:
         # don't know the index of i

However, you can use enumerate to keep trace of the index - now it is easy

for idx, i in enumerate(lst):
    if i % 2 == 0:
        lst[idx] = 0

Usually it's simpler to use a list comprehension and replace the whole list with a new one.

You can test the lowest bit of each number using the bitwise and &

>>> L = [1, 2, 3, 4, 5] 
>>> L = [x if x & 1 else 0 for x in L]
>>> L
[1, 0, 3, 0, 5]

Depending on your CPU and the Python implementation, you may find this is faster (but less readable in my opinion):

[x * (x & 1) for x in L]

You can do this with a list comprehension like so:

list = [x * (x%2) for x in list]
print list

Notice that x%2 can only result in 1 or 0. Hence, if the number is even, the result of multiplication is 0, and if it is odd, it is the number itself.

Demo

>>> my_list = [1,2,3,4,5]
>>> def myfun(x):
    if x % 2 != 0:
        return x
    else:
        return 0


>>> list(map(myfun, my_list))
[1, 0, 3, 0, 5]

Try this:

my_list = [1,2,3,4,5]
my_result = list()

for number in my_list:
    if number % 2 == 0:
        my_result.append(0)
    else:
        my_result.append(number)

print my_result

You can also use list comprehensions :

my_list = [1,2,3,4,5]
print [0 if x%2==0 else x for x in my_list]

Output is the same for both examples:

[1, 0, 3, 0, 5]

It is important to highlight that list is a Python reserved word for its list data structure. That's why I assign the list a different name.


Documentation:

List comprehensions: https://docs.python.org/2/tutorial/datastructures.html#list-comprehensions

Using List Comprehension and ternary method :

list1 = [1,2,3,4,5]
list1[:] = [a if a%2 != 0 else 0 for a in list1 ] #  [i if i % 2 == 1 else 0 for i in L]
print list1

Output:

[1, 0, 3, 0, 5]

Notes:

  • By list1[:] I am not creating a new list I am adding to the already existing List
  • What I have done in my LC is I checked if the value is odd then given the same value if not I have given 0

Or for normal method

Code1:

list1 = [1,2,3,4,5]
for i,v in enumerate(list1):    
    if v%2==0:
        list1[i]=0
print list1

Output:

[1, 0, 3, 0, 5]
lst = [1,2,3,4,5]
# List comp
[x if x % 2 else 0 for x in lst] # when x % 2 is 0 if returns false

# for-loop
for i, x in enumerate(lst):
    if x % 2 == 0:
        lst[i] = 0

#Output
[1, 0, 3, 0, 5]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM