6 ms·
You've made a mistake with cut'n'paste, methinks. Actually, it is: >>> state = [1, 2, 3] >>> states = [4, 5, 6] >>> [st for s in state for st in states]
by jhdevos 15y ago
You've made a mistake with cut'n'paste, methinks. Actually, it is:
>>> state = [1, 2, 3]
>>> states = [4, 5, 6]
>>> [st for s in state for st in states]
[4, 5, 6, 4, 5, 6, 4, 5, 6]
- Herald_MJ 15y agoSo the code can be simplified as >>> states * len(state)
- llambda 15y agoJust the example. But this example is just to illustrate what's going on. It's not the same as what the author is trying to achieve.
- masklinn 15y agoThis code yes, in TFA's code the inner iteration (on `states`) depends on the result of the outer iteration (on `state`). For what it's worth, it's pretty easy to "unwrap" a Python listcomp: it's a sequence of nested loops, nested tests and a mapping. result = [mapping for iteration1 for iteration2 for iteration3 if test1 if test1] is equivalent (modulo some scoping interaction with the code outside it) to result = [] for iteration1 for iteration2 for iteration3 if test1 if test2 result.push(mapping) Apart from the final mapping, listcomps are executed strictly left to right, an evaluation can use anything produced to the "left" of it.