Python None, Null, and Zero

13153
3
Jump to solution
12-30-2013 07:56 AM
NickLegg
New Contributor
Hi There,

I'm performing some calculations in a table where I want to filter out Null values, but keep values of zero. I am trying to use the Python Filter function, but am having some issues. When I specify that values of "None" be filtered, zero values are also filtered. I would like to only filter Null values. This is confusing me because any literature I read says that the Python None value is equal to Null.

Here's a simple form of my Python script (Pre-logic Script) in field calculator:

def trial(f1,f2):
   list = [f1,f2]
   l1 = len(filter(None,list))
   return l1

The code above runs fine, but returns undesired lengths when f1 or f2 are zero.

Can anyone tell me what I am doing wrong, or another simple way of filtering out Null values and not zero values? Thanks for your insight!

Happy New Year!
Nick
0 Kudos
1 Solution

Accepted Solutions
T__WayneWhitley
Frequent Contributor
Think I'd just do it like this:
def trial(f1,f2):      list = [f1,f2]      l1 = len([i for i in list if i is not None])      return l1



Wayne


PS - Incidentally, if you had an undetermined number of values, say f1, f2, f3, f4,... , then you could feed in the whole list, as in:
def trial(theList):      l1 = len([i for i in theList if i is not None])      return l1


...so that, for example, you could call it as such (this example simply has 3 items in a list):
>>> print list # a predefined list [0, None, 3] >>>  >>> trial(list) 2 >>> 



...and, since I've never actually used 'filter' or 'lamda' before, here is another alternative - and I'm not sure I'd use this or if it can be written shorter, but certainly an alternative (which can also be rewritten to accept a list):
>>> print f1, f2 0 None   >>> def trial(f1, f2):  list = [f1, f2]  l1 = len(filter(lambda x: x>=0, list))  return l1  >>> trial(f1, f2) 1 >>> 

View solution in original post

0 Kudos
3 Replies
T__WayneWhitley
Frequent Contributor
Think I'd just do it like this:
def trial(f1,f2):      list = [f1,f2]      l1 = len([i for i in list if i is not None])      return l1



Wayne


PS - Incidentally, if you had an undetermined number of values, say f1, f2, f3, f4,... , then you could feed in the whole list, as in:
def trial(theList):      l1 = len([i for i in theList if i is not None])      return l1


...so that, for example, you could call it as such (this example simply has 3 items in a list):
>>> print list # a predefined list [0, None, 3] >>>  >>> trial(list) 2 >>> 



...and, since I've never actually used 'filter' or 'lamda' before, here is another alternative - and I'm not sure I'd use this or if it can be written shorter, but certainly an alternative (which can also be rewritten to accept a list):
>>> print f1, f2 0 None   >>> def trial(f1, f2):  list = [f1, f2]  l1 = len(filter(lambda x: x>=0, list))  return l1  >>> trial(f1, f2) 1 >>> 
0 Kudos
NickLegg
New Contributor
That worked perfect! Thanks Wayne.
0 Kudos
T__WayneWhitley
Frequent Contributor
Happy New Year...and one of the 'renewed' resolutions I have is to remember to correct more of my brain farts, such as this one in my above post:

lambda x: x>=0


To be equivalent to what I posted in the preceding code block:

i for i in theList if i is not None


...then I should have posted:

lambda x: x is not None


(or, to repost the entire code with the correction):
>>> print f1, f2
0 None
 
>>> def trial(f1, f2):
 list = [f1, f2]
 l1 = len(filter(lambda x: x is not None, list))
 return l1

>>> trial(f1, f2)
1
>>>


That way, whatever was plugged into the initial solution, the list comprehension expression (including any text strings or negative numbers, for example), works in the same fashion with lambda x.  (My sample input, 0 and None, wasn't broad enough to show this.)

Just FYI...lol, don't know why I didn't just keep it consistent, like I said: brain fart!


Wayne
0 Kudos