News for hackers. Stuff that matters.
Re:Strange line of Python by lloeki on Friday November 18, 2011 @07:14AM (#3251480)
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

[1] http://stackoverflow.com/questions/1016997/generate-from-gen...

Re:Strange line of Python by buro9 on Friday November 18, 2011 @07:30AM (#3251515)
It 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
                  )
         );
Re:Strange line of Python by ghoul2 on Friday November 18, 2011 @05:17PM (#3253721)
find the store_types such that at least one store of that type exists in at least one city?

this is probably nerd sniping :(

Re:Strange line of Python by masklinn on Friday November 18, 2011 @07:24AM (#3251498)
Exactly, 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`.

Re:Strange line of Python by llambda on Friday November 18, 2011 @07:27AM (#3251507)
It 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.