Re: How to replace the values with keys ?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Fredrik Lundh

    Re: How to replace the values with keys ?

    "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>

  • SvaPs

    #2
    Re: How to replace the values with keys ?

    Hi all..,

    I tried with this..

    def multiple_replac e(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_replac e(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_replac e
    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>

    Comment

    Working...