Test 0 and false since false is 0

zhenghao li zheng.228 at hotmail.com
Thu Jul 6 22:48:02 EDT 2017


you can use the "is" for identity test.
l1 = [v for v in array if not v is 0]
l2 = [v for v in array if v is 0]

On Jul 6, 2017, at 10:31 PM, Sayth Renshaw <flebber.crue at gmail.com<mailto:flebber.crue at gmail.com>> wrote:

I was trying to solve a problem and cannot determine how to filter 0's but not false.

Given a list like this
["a",0,0,"b",None,"c","d",0,1,False,0,1,0,3,[],0,1,9,0,0,{},0,0,9]

I want to be able to return this list
["a","b",None,"c","d",1,False,1,3,[],1,9,{},9,0,0,0,0,0,0,0,0,0,0]

However if I filter like this

def move_zeros(array):
   l1 = [v for v in array if v != 0]
   l2 = [v for v in array if v == 0]
   return l1 + l2

I get this
['a', 'b', None, 'c', 'd', 1, 1, 3, [], 1, 9, {}, 9, 0, 0, 0, False, 0, 0, 0, 0, 0, 0, 0]

I have tried or conditions of v == False etc but then the 0's being false also aren't moved. How can you check this at once?


Cheers

Sayth
--
https://mail.python.org/mailman/listinfo/python-list


More information about the Python-list mailing list