My first Python program -- a lexer

John Machin sjmachin at lexicon.net
Sun Nov 23 22:58:32 CET 2008


On Nov 12, 2:54 am, Thomas Mlynarczyk <tho... at mlynarczyk-webdesign.de>
wrote:
> John Machin schrieb:
>
> > You are getting closer. A better analogy is that using a dict is like
> > transporting passengers along an autobahn in an aeroplane or
> > helicopter that never leaves the ground.
>
> It is not a bad idea to transport passengers in an airplane, but then
> the airplane should not follow an autobahn, but use the shortest way --
> at an appropriate altitude. Translated back to Python dicts, this would
> mean that using a dict for my purposes is a good idea, but that I do not
> make use of its full capabilities -- in other words, I should rewrite my
> code -- still using dict but in a better way. Or do you mean that for 10
> kilometers of autobahn, an airplane would be overkill?

That is definitely what I mean. Sorry for not replying before; I
thought that this was a rhetorical question ;-)

>
> Maybe I am a bit biased by my PHP background, but { name: regex, ... }
> looks simpler to me than [ ( name, regex ), ... ], because the former is
> not a nested structure, while the latter would be a 2D-array in PHP.
>
> Suppose I use the dict and I want to access the regex associatetd with
> the token named "tokenname" (that is, no iteration, but a single
> access). I could simple write tokendict["tokenname"]. But with the list
> of tuples, I can't think of an equally easy way to do that.

If you need a predictable and easily specified order of testing a
string against multiple regexes, a list of tuples is indicated.

*IF* you need to access the regex associated with a token in O(1)
time, a dict is indicated.

If you have *both* requirements simultaneously, then *both* list and
dict are indicated.



More information about the Python-list mailing list