3 ms·
If I understand correctly what is asked: > At first it looks just like a double loop. But then you notice the right-most for for st > is taking the list
by lloeki 15y ago
If I understand correctly what is asked:
> At first it looks just like a double loop. But then you notice the right-most for
for st
> is taking the list to iterate over from its own body,
in states[(s,letter)
> which is the iterator of the left-most for loop
for s in state
Well I don't get what's weird (or I'm actually missing something in the post), it actually IS a double loop. [0] says:
Only the outermost for-expression is evaluated immediately, the other expressions are deferred until the generator is run:
g = (tgtexp for var1 in exp1 if exp2 for var2 in exp3 if expo)
is equivalent to:
def __gen(bound_exp):
for var1 in bound_exp:
if exp2:
for var2 in exp3:
if exp4:
yield tgtexp
g = __gen(iter(exp1))
del __gen
So exp2, 3, 4 can each depend on the previous levels var1, 2, 3...
I wrote something like that in a comment on SO[1]
return reduce(lambda a, v: (x for v in a for x in kidsFunc(v)), xrange(generation), [val])
[0] http://www.python.org/dev/peps/pep-0289/#the-details http://www.python.org/dev/peps/pep-0289/#the-details
[1] http://stackoverflow.com/questions/1016997/generate-from-generators/1017105#1017105 http://stackoverflow.com/questions/1016997/generate-from-gen...
- masklinn 15y agoExactly, it's a nested iteration: the rightmost loop is just nested within the leftmost loop, so it's iterating on `states[(s,letter)]` for each value (s) of `state`. The weirdest part, to me, is around that: it's surrounded by some sort of manual reduction, which changes the current `state` content by iterating over `word`.
- llambda 15y agoIt most certainly is a double loop. I can understand how it seems confusing but once you look at what's going on it really isn't all that complicated. Sure the syntax is a little less clear (due to the list comprehension, I think Guido is probably cringing right now) but the logic is fairly straightforward.
- buro9 15y agoIt reminded me of the SQL double NOT EXISTS: SELECT DISTINCT store_type FROM stores WHERE NOT EXISTS ( SELECT * FROM cities WHERE NOT EXISTS ( SELECT * FROM cities_stores WHERE cities_stores.city = cities.city AND cities_stores.store_type = stores.store_type ) );
- ghoul2 15y agofind the store_types such that at least one store of that type exists in at least one city? this is probably nerd sniping :(