I know the right way to have multiple for
in a nested list comprehension is as follows (Python 3):
lista = [[[1,2],[3],[4,5,6]],[[7],[8,9]]]
flatlista = [i for k in lista for j in k for i in j]
# results with [1, 2, 3, 4, 5, 6, 7, 8, 9]
But my natural language instincts strongly object. I would have (wrongly) expected the code to be:
flatlista = [i for i in j for j in k for k in lista]
The wrong version sounds almost like English and is read in one stream left to right. The correct version requires some nested reading skills skipping left and right to encompass the meaning.
Why is this syntax as it is? Why was the language built this way?
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…