simple question on dictionary usage

Frank Stutzman stutzman at skywagon.kjsl.com
Sat Oct 27 12:17:43 EDT 2007


Wow, what a great group!  Lots of useful and kind suggestions to what I was
sure was a fairly dumb question.  A few comments on some selected suggestions
(but I appreciate all of them)

Edward Kozlowski wrote:

> egt = {}
> for key in record:
>    if key.startswith('E'):
>        egt[key] = record[key]

I actually had come up with something like this, but thought it wasn't
quite as pythonish as it should be.  It is certainly much more readable
to a neophyte to python.
 
Bruno Desthuilliers wrote:

> egt = dict((k, v) for k, v in record.iteritems() if k.startswith('E'))

This is what I was looking for.  I thought I had seen something simular
to this in one of the tutorials I had read, but couldn't seem to find it.

Steven D'Aprano:

> eval("{" + reduce(lambda x,y: y+', '+x, [mo.group(1) for mo in __import__
> ('re').finditer(r"('E.*?'\s*:\s*'.*?'),?", str(record))], "") + "}")

Ah!  Now this is one solution I can get my teeth into.  If its not obvious,
I'm a recovering perl programmer.

Thanks to all
 

-- 
Frank Stutzman






More information about the Python-list mailing list