3 ms·
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 s
by danieldk 15y ago
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.
- vog 15y agoIndeed, naming this the "strangest line of python" is a hopeless exaggeration. For a really strange line of Python, have a look at the solutions to the pyContest 2005: http://www.pycontest.net/ranking http://www.pycontest.net/ranking (HN entry: http://news.ycombinator.com/item?id=3251805 http://news.ycombinator.com/item?id=3251805)
- pak 15y agoYou made a great point, indirectly; a lot of the confusion from the original line of code is the crappy naming of the variables. They should have thrown in sta and stat to really amp up the Scrabble-like atmosphere. Once you clearly distinguish the purpose of each variable with a well chosen name, as you did in your rewrite, the code suddenly lights up the room. Good variable names are important! The linebreak to break up the three-way expression also helps to suggest the order of evaluation. For some reason, our brains do not appreciate nested ternary operators on the same line. It's the same problem as A==B ? C : D==E ? F : G; If you are familiar with the left- or right-associativity of the operator, this isn't impossible to read. For instance, if you write C, you already know it's right-associative and therefore executes much like it reads from left to right. If so, it's still nicer to convey that with some line breaks: A==B ? C : D==E ? F : G ; On the other hand, if you are using a language like PHP where it's left-associative, or you're not 100% positive either way, you will be shooting yourself in the foot trying to do something like that and brilliantly misleading anybody else that reads the code.
- wisty 15y ago> What's so hard about this? I've been programming python for quite a while, and never seen anything like main_iterator = 'abc' inner_iterator = '123' [c+n for c in main_iterator for n in inner_iterator] It's just ugly. I guess it has its purpose (cross products?) but it's still ugly. Once you know what that means, it's obvious what you mean by [j for i in main_iterator for j in inner_iterator(i)] But I've just never seen two loops crushed together like this.