0

I got a list of lists with integers and Nones. I want to filter out only the Nones.

[int(all(sub)) for sub in zip(*lists_for_filtering)]

This removes the None's, BUT also the 0 (integer). I want to modify it in the way to keep 0 (integer)

Result is a list list [1,0,1,0,1,0,1,0] --> Nones result in 0, but also 0 results in 0 --> this should result in 1 because its not None

  • Care to show `lists_for_filtering` ? – Arkistarvh Kltzuonstev Jun 07 '19 at 09:52
  • [1,2,3,None,5,4,3,0,None] ... 4 lists like this. –  Jun 07 '19 at 09:52
  • Suppose, `lff = [[3, 0, None, 4], [6, 7, 0, None], [8, 0, 5, 6], [4, None, 8, 6]]`. You could use this list-comprehension to remove NoneTypes : `[[i for i in l if i!=None] for l in lff]`. Gives : `[[3, 0, 4], [6, 7, 0], [8, 0, 5, 6], [4, 8, 6] ]` – Arkistarvh Kltzuonstev Jun 07 '19 at 09:59
  • Here's one possible solution: `[[int(x) for x in sub if x is not None] for sub in zip(*lists_for_filtering)]`. And by the way, using `zip` will transform your list structure, is this what you want on purpose? – Cvetan Mihaylov Jun 07 '19 at 10:17

2 Answers2

0

If I understand correctly, your problem is the all() function, that evaluates both 0 and None as False. Write your own boolean filter that only evaluates None as False and try this again. Do something like this:

def noNone(seq):
    if all(map(lambda x: x != None, seq)):
        return True
    else:
        return False 
CLpragmatics
  • 625
  • 6
  • 21
0

What you want can be obtained with an explicit comprehension instead of all, because all tests values to False and both 0 and None eval to False in a boolean context:

[int(all((a is not None for a in sub))) for sub in zip(*lists_for_filtering)]
Serge Ballesta
  • 143,923
  • 11
  • 122
  • 252