By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,386 Members | 1,676 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,386 IT Pros & Developers. It's quick & easy.

Re: How to replace the values with keys ?

P: n/a
"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>

Jul 23 '08 #1
Share this Question
Share on Google+
1 Reply


P: n/a
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>
Jul 23 '08 #2

This discussion thread is closed

Replies have been disabled for this discussion.