Idiom for flattening a shallow nested list: how does it work?

前端 未结 4 963
闹比i
闹比i 2021-01-05 10:57

I found this bit of code in a module I am working on:

l = opaque_function()
thingys = [x for y in l for x in y]

I can\'t read this. By expe

4条回答
  •  太阳男子
    2021-01-05 11:42

    You should read this as:

    for y in l:
        for x in y:
            yield x
    

    That's the generator version, but all comprehensions have the same basic syntax: while the x is put up front, the rest of the expression is still read left-to-right. I was confused by this at first too, expecting it to be the other way around, but it makes sense once you add filtering expressions:

    >>> l = [[1,2,3,4,5], [1,"foo","bar"], [2,3]]
    >>> [x for y in l
    ...    if len(y) < 4
    ...    for x in y
    ...    if isinstance(x, int)]
    [1, 2, 3]
    

    Now imagine having to write this entire thing backwards:

    [x if isinstance(x, int)
       for x in y
       if len(y) < 4
       for y in l]
    

    That would be confusing even to veteran Prolog programmers, not to mention the people maintaining Python parsers :)

    The current syntax also matches that in Haskell, which inspired list comprehensions in the first place.

提交回复
热议问题