I created the following list comprehension in python:
[int(a[0].internal_value).lower() if type(a[0].internal_value) in (str,unicode) and a[0].internal_value.isdigit() == True
else str(a[0].internal_value).lower() if type(a[0].internal_value) in (str,unicode)
else int(a[0].internal_value) if type(a[0].internal_value) in (float,int)
for a in ws.iter_rows() if a[0].internal_value <> None]
I'm having issues trying to construct the final else, if condition:
else int(a[0].internal_value) if type(a[0].internal_value) in (float,int)
I get an invalid syntax if I use the if conditional in that line.
if type(a[0].internal_value) in (float,int)
If I remove the if statement
else int(a[0].internal_value)
then it seems to run fine. I need to have that if statement in there.
To me the else, if conditions are list comprehensions way of doing the more simple if, else conditions:
if i == x:
do something
elif i == y:
do something
elif i == z:
do something
By rule, you do not always have to have an 'else' to close a series of conditional sentences. It seems to me, that my code wants a final 'else' in the comprehension. Am I correct in stating that and if so, is there a way to construct a final else, if in a python list comprehension instead of a final else?
<>has been deprecated in favour of!=; the deprecated operator has been removed from Python 3 altogether.[ x*x for x in range(10) ], not this. Listen to Joran...