help please
Steven Bethard
steven.bethard at gmail.com
Mon Feb 21 00:02:08 EST 2005
gargonx wrote:
> Even if i put it in exactly the way you did:
>
>>>>import re
>>>>charmatcher = re.compile(r' [A-Z] [\d]?')
>>>>
>>>>ext = dict(D="V1", O="M1", G="S1")
>>>>std = dict(S="H")
>>>>
>>>>decode_replacements ={}
>>>>decode_replacements.update([(std[key], key) for key in std])
>
> Traceback (most recent call last):
> File "<stdin>", line 1, in ?
> AttributeError: keys
What version of Python are you using? Here's what I get:
PythonWin 2.4 (#60, Nov 30 2004, 09:34:21) [MSC v.1310 32 bit (Intel)]
on win32.
Portions Copyright 1994-2004 Mark Hammond (mhammond at skippinet.com.au) -
see 'Help/About PythonWin' for further copyright information.
py>
py> import re
py> charmatcher = re.compile(r' [A-Z] [\d]?')
py>
py> ext = dict(D="V1", O="M1", G="S1")
py> std = dict(S="H")
py>
py> decode_replacements = {}
py> decode_replacements.update([(std[key], key) for key in std])
As you can see, I'm using Python 2.4. I'm guessing you're using an
earlier version. I just checked the docs for dict.update:
"update() accepts either another mapping object or an iterable of
key/value pairs (as a tuple or other iterable of length two). If keyword
arguments are specified, the mapping is then is updated with those
key/value pairs: "d.update(red=1, blue=2)". Changed in version 2.4:
Allowed the argument to be an iterable of key/value pairs and allowed
keyword arguments."
So dict.update() only accepts a list of key/value pairs as of 2.4 I
guess. Try:
py> decode_replacements.update(dict([(std[key], key) for key in std]))
I believe that, unlike dict.update, the dict constructor allowed a list
of key/value pairs previous to Python 2.4. If that doesn't work either,
you can do the more verbose version:
py> for key in std:
... decode_replacements[std[key]] = key
...
STeVe
More information about the Python-list
mailing list