News for hackers. Stuff that matters.
Re:Strange line of Python by tudorachim on Friday November 18, 2011 @06:57AM (#3251448)

    state = [st for s in state for st in states[(s,letter)]]
can be read as

    new_state = []
    for s in state:
        for st in states[(s, letter)]:
            new_state.append(st)
    state = new_state
Re:Strange line of Python by acqq on Friday November 18, 2011 @09:19AM (#3251772)
And here is an example how it actually runs. I did some renames to make more obvious what's where:

   something = [ x for loop ]
Means

   something = []
   for loop:
       something.append[ x ]
so I'll use x for the current element of the resulting list (I'll rename st to x).

Second, the 'letter' is not relevant for analysis, I'll assume a constant.

Third, there are variables 'state' and 'states' so naming again can confusingly suggest that there is some deep connection between them. I'll rename 'states' to 'moves'. So instead of:

    state = [st for s in state for st in states[(s,letter)]]
I'll start from:

    state = [x for s in state for x in moves[s]]
Now to show exactly what's going on, I'll rewrite it to print partial results:

    state=[0,1,2]
    moves=[[11,12],[21,22],[31,32]]
    t = []
    for s in state:
        a = moves[ s ]
        print "s, a:",s, a
        for x in a:
            print "x:", x
            t.append( x )
    print t
    state = t
The output is:

    s, a: 0 [11, 12]
    x: 11
    x: 12
    s, a: 1 [21, 22]
    x: 21
    x: 22
    s, a: 2 [31, 32]
    x: 31
    x: 32
    [11, 12, 21, 22, 31, 32]
Re:Strange line of Python by lmm on Friday November 18, 2011 @08:35AM (#3251648)
I'm a big python user and I didn't realize this. It seems like the nesting order for list comprehensions is backwards? I would naturally read the line as state = [st for s in [state for st in states[(s,letter)]]] because that's the way the natural language brackets, but this line is obviously broken. I would expect the syntax for the for loop you write to look like "state = [st for st in states(s, letter) for s in state]", which I think reads very clearly and understandably. So can anyone explain why the nesting works the way it does?
Re:Strange line of Python by divtxt on Friday November 18, 2011 @08:41AM (#3251661)
I too expected the syntax list comprehensions to chained.

Turns out that nesting is a feature:

http://www.python.org/dev/peps/pep-0202/

- The form [... for x... for y...] nests, with the last index varying fastest, just like nested for loops.

Re:Strange line of Python by ajanuary on Friday November 18, 2011 @08:58AM (#3251711)
My background is with LINQ, which means I find it more natural to group it as:

st {for s in state} {for st in states[(s,letter)]}

But I agree, reading it as English your grouping makes more sense.

Re:Strange line of Python by evmar on Friday November 18, 2011 @10:38AM (#3252031)
This got me too -- I thought the reason the code was "the strangest" was because the double-loop was nested the wrong way.