I just stumbled over what seems to be a flaw in the python syntax-- or else I'm missing something.
See this:
[x for x in range(30) if x % 2 == 0]
But this is a syntax error:
[x for x in range(30) if x % 2 == 0 else 5]
If you have an else
clause, you have to write:
[x if x % 2 == 0 else 5 for x in range (30)]
But this is a syntax error:
[x if x %2 == 0 for x in range(30)]
What am I missing? Why is this so inconsistent?
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…