How to replace the values with keys ?

SvaPs hiursvapsisnowhere at gmail.com
Wed Jul 23 14:31:04 CEST 2008


Hi all..,

I tried with this..

def multiple_replace(text,adict):
    rx=re.compile('|'.join(map(re.escape,adict)))
    def one_xlat(match):
            return adict[match.group(0)]
    return rx.sub(one_xlat,text)

print multiple_replace(text,adict)

I am thrown with a error:
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "<stdin>", line 2, in multiple_replace
TypeError: argument 2 to map() must support iteration


I would be grateful for any comments..

Thankin in advance'
GraPs.


Fredrik Lundh wrote:
> "Graps Graps" wrote:
>
> > Text2 is a python dictionary containing data as
> >
> > {0: 'a', 1: 'b', 2: 'c'...}
> >
> > now I want the data in text1 to be replaced with the keys, say like
> >
> > 0 1
> > 0 2
> > 0 3
> > 0 6
> > 1 2... so on..
>
> someone asked a very similar question not long ago, so maybe you're
> really supposed to figure this out yourself.  I'm sure your teacher
> won't kill you if you ask for a hint.
>
> (if you asked me for a hint, I'd tell you to look for questions about
> dictionaries in the mailing list archives).
>
> </F>



More information about the Python-list mailing list