I was testing the all() and any() functions to make sure I understood how they operate. Turns out I do not understand them. Here is my test code:
v1=[True, False, True] # unexpected 2
v2=[True] # wai
v3=[] # unexpected 2
v4=[False] # wai
v5=[False,False] # wai
v6=[False,False, True] # unexpected 2
v7=[False]*4 # wai
v8=[float('nan'),False]# unexpected 1, but I think this is because any() considers "truthy" values. Given this, 2 is unexpected.
v9=['f','t'] # wai, given "truthy" values. Would like this to return False.
v10=['random_string'] # same as above
vlist=[v1,v2,v3,v4,v5,v6,v7,v8,v9,v10]
for v in vlist:
print('any true:',any(v)==True,'| not all false:',not(all(v)==False),'(',v,')')
Returns:
any true: True | not all false: False ( [True, False, True] )
any true: True | not all false: True ( [True] )
any true: False | not all false: True ( [] )
any true: False | not all false: False ( [False] )
any true: False | not all false: False ( [False, False] )
any true: True | not all false: False ( [False, False, True] )
any true: False | not all false: False ( [False, False, False, False] )
any true: True | not all false: False ( [nan, False] )
any true: True | not all false: True ( ['f', 't'] )
any true: True | not all false: True ( ['random_string'] )
Where I've coded the output to display the logical summary of the statement (ex: any true), followed by the evaluated value. This is done for both statements, and then the parenthetical at the end shows the list evaluated.
Most of my surprises come from the not all()
combination. But I was also expecting non-true values (ex: float('nan') or 'random_string') to be false. Looking at the documentation I see that this is not the case, but I would like to know how to make this an option.
I attempted to code tests for the logical statements: "are any True?" and "not: are all False?". If I'm not mistaken, these should be logically identical.
Some more detailed explanations for my surprises. I refer to the "inside" statement as all(v)==False
and the negation of this being the outside statement:
v1=[True, False, True]
- For the inside statement, I consider whether all elements are False. This is clearly not the case, so I expect the statement to be false. The outsidenot
negates this, so the result of this should be True.v3=[]
- The set is empty. Thus all elements are false is True, and not this should be false.v6=[False,False, True]
- It's clear that there are some True elements, so the inside statement is False. Not this, means it should be True.v8=[float('nan'),False]
- This one is a puzzle for me. I'd like 'nan' to read as False (only accept True values), but looking at the documentation this is not how it works. Given this piece of information, it is unexpected that the second statement reads False, since 'nan' is a "truthy" value. So all elements are False is a false statement, and negated I'd expect the output to be True.v9
andv10
Are working as expected once I learned about the "truthy" values. However, I'd like them to read False under both circumstances (considering only1
orTrue
as a True value, others as False). Of particular concern to my current project is reading 'nan','NaN',etc. as False.