Why is the order of multiple `for` list comprehension the way it is?

前端 未结 2 448
天涯浪人
天涯浪人 2020-12-18 15:41

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]]]

fl         


        
2条回答
  •  春和景丽
    2020-12-18 16:06

    Because that's what PEP 202 -- List Comprehensions set it to. The PEP doesn't quite motivate why however, as it was created as an afterthought; the discussion had taken place on the development lists years before the PEP was created, or even the PEP process had been created.

    First of all, the order mirrors the order you'd nest for loops and if statements in Python code:

    for k in lista:
        for j in k:
            for i in j:
    

    This makes it very natural if you are already used to that ordering.

    Looking at the very first discussions about the feature there appears to be precedent in other languages for the ordering. And indeed, Haskell has the same ordering: each successive generator refines the results of the previous generator.

    Certainly, at some point Tim Peters (the originator of the proposal) states that the order used today is obvious to him, see this post:

    I've posted my proposed translation into today's Python a few times already, with the intent that it be taken literally, not suggestively. This nests "for" loops with the leftmost outermost, so nails everything about the ordering semantics at all levels. Why that's become a debating point at all levels is beyond me .

提交回复
热议问题