state = [st for s in state for st in states[(s,letter)]]
What's so hard about this?- states is a dict with transitions. The key is a tuple of a state and a symbol. If the value is a non-empty list, that means there is an outgoing arc from the state to another state with that symbol.
- state is the list of states you are currently in (remember, this is an NFA, so you can be in more than one state).
So, what this fragments declaratively says: the new list of states is obtained by following the outgoing arcs with the given symbol from the current list of states.
The comprehension would be more comprehensible (no pun intended) like this:
newStates = [newState for curState in curStates
for newState in transitions[(curState,letter)]]
# curStates = newStates
Edit: line break to avoid the need to scroll.