7 ms·
Could someone please explain how this statement works: > set(e2 for e1 in edits1(word) for e2 in edits1(e1)) I understand how you can say something like "f(x)
by rand_r 14y ago
Could someone please explain how this statement works:
> set(e2 for e1 in edits1(word) for e2 in edits1(e1))
I understand how you can say something like "f(x) for x in edits1(word)", but the way it's used above with multiple fors is making my brain hurt.
- deleted 14y ago[deleted]
- seanl 14y agoSomething roughly equivalent to: s = set() for e1 in edits1(word): for e2 in edits1(e1): s.add(e2)
- textminer 14y agoThe one bit of sugar in Python that always throws me off. Since the inner argument is written first in the comprehension, my intuition is that I'll add the iteration statements as I expand out to a slightly-higher loop. Nope!
- andreasvc 14y agoIt's not necessarily the inner argument that's written first: >>> [a for a in range(3) for b in range(2)] [0, 0, 1, 1, 2, 2] It's just that for every iteration of the inner loop, it is evaluated and added to the result. Once you keep in mind that their order has the same meaning as with normal for loops it's not so tricky. You can even indent them that way to make that more obvious.
- deleted 14y ago[deleted]