Add zero but not False to python list

I'm trying to move all zeros in the list to the end of the line, my only problem is that the list has False bool. I just found out that False == 0, since I move all zeros to the end of the list and keep false intact?

def move_zeros(array): #your code here for i in array: if i == 0: array.remove(i) array.append(i) answer = array print answer 

move_zeros(["a",0,0,"b",None,"c","d",0,1,False,0,1,0,3,[],0,1,9,0,0,{},0,0,9])

This is what is returned at startup.

 ['a', 'b', None, 'c', 'd', 1, 1, 3, [], 1, 9, {}, 9, 0, 0, 0, 0, 0, False, 0, 0, False, 0, 0] 
+6
source share
9 answers

What you do is normal sorting. Therefore, simply implement it as follows:

 array.sort(key=lambda item: item is 0) 

What this means: "Convert the array to logical, where elements that are 0 are True and everything else is False." Then sorting these booleans puts all the False values ​​on the left (because they are 0), and the True values ​​on the right (for example, 1).


I originally wrote a solution that is not supported in Python 3:

 array.sort(lambda L,R: -1 if R is 0 else 0) 

This means that "L is less than R if R is 0". Then we sort according to this. So we end up with zeros on the right, because something is smaller than them. The above only works on Python 2.

+8
source

You can use sorted :

 sorted(array, key=lambda x: x is 0) 
+4
source

Here is one way. Note. False == 0 - True , but False is 0 - False .

 >>> l = ["a",0,0,"b",None,"c","d",0,1,False,0,1,0,3,[],0,1,9,0,0,{},0,0,9] >>> [x for x in l if x is not 0] + [x for x in l if x is 0] ['a', 'b', None, 'c', 'd', 1, False, 1, 3, [], 1, 9, {}, 9, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0] 

EDIT: This is worse than sorted solutions, less elegant, and sorted through the list.

+3
source
 for i in array: if i == 0 and i is not false: array.remove(i) array.append(i) answer = array print answer 
0
source

if i == 0 and type(i) is int :

You can do it higher. The whole program is as follows:

 def move_zeros(array): #your code here for i in array: if i == 0 and type(i) is int: array.remove(i) array.append(i) answer = array print answer 
0
source

Since list sorting is stable, you can do

 array.sort(key=(lambda x: 1 if (x==0 and x is not False) else 0)) 

comparing an identifier by numbers ( x is 0 ) is dangerous because, although it works fine, there is no guarantee.

Using key makes sorting much faster.

Actually, you can also do

 sorted_array=[] zeroes=0 for e in array: if e==0 and e is not False: zeroes+=1 else: sorted_array.append(e) sorted_array.extend([0]*zeroes) 

which theoretically should be less work, but in practice it is much slower

0
source

I read from this post that Python treats 0 as a logical False. Therefore, it will always be placed at the end of your arrays, for example, 0. I suggest casting boolean False as a String type first, "False", so it will not be affected by your type.

my_list = ["a", 0,0, "b", Nothing, "c", "d", 0,1, 'False', 0,1,0,3, [], 0,1, 9, 0,0, {}, 0,0,9]

If you need to use it as boolean just enter it as bool:

 false = False print type(false) //output is <type 'bool'> 
0
source

Below is the linear scanning algorithm O (n), in contrast to the already described algorithms O (nlogn) and O (nn). Move non-0 items only once and add 0 to one batch.

 def move_zeros(array): "Mutate list by putting int 0 items at the end, in O(n) time." dest = 0 zeros = 0 for item in array: if not item and type(item) is int: # int 0 zeros += 1 else: if zeros: array[dest] = item dest += 1 array[-zeros:] = [0] * zeros inn = ["a",0,0,"b",None,"c","d",0,1,False,0,1,0,3,[],0,1,9,0,0,{},0,0,9] move_zeros(inn) print(inn) # ['a', 'b', None, 'c', 'd', 1, False, 1, 3, [], 1, 9, {}, 9, # 0, 0, 0, 0, 0, 0, 0, 0, 0, 0] 
0
source

I know this question already has a bunch of answers, but for future readers, I'm just going to add my two cents to it. Most answers do not cover the case of float 0.0 , which is also zero. I am going to use the idea of ​​the above answers in my own code.

Note. My decision may not be as elegant as other experts.

 def move_zeros(array): # your code here if len(array) < 1: return array else: list = [] zeros = 0 for i in array: if not i and type(i) is int or type(i) is float: zeros += 1 else: list.append(i) for i in range(0, zeros): list.append(0) return list 

It has been tested with 25 different test cases.

0
source

All Articles