2

I'm trying to find the best way to do the following:

I have a string lets say:

str = "pkm adp"

and I have a certain code in a dictionary to replace each charecter such as this one:

code =  {'a': 'c', 'd': 'a', 'p': 'r', 'k': 'e', 'm': 'd'}

('a' should be replaced by 'c', 'd' by 'a' ...)

How can I convert the first string using the required characters from the dictionary to get the new string? Here for example I should get "red car" as the new string.

6 Answers 6

8

Try this:

>>> import string
>>> code = {'a': 'c', 'd': 'a', 'p': 'r', 'k': 'e', 'm': 'd'}
>>> trans = string.maketrans(*["".join(x) for x in zip(*code.items())])
>>> str = "pkm adp"
>>> str.translate(trans)
'red car'

Explanation:

>>> help(str.translate)
Help on built-in function translate:

translate(...)
    S.translate(table [,deletechars]) -> string

    Return a copy of the string S, where all characters occurring
    in the optional argument deletechars are removed, and the
    remaining characters have been mapped through the given
    translation table, which must be a string of length 256.

>>> help(string.maketrans)
Help on built-in function maketrans in module strop:

maketrans(...)
    maketrans(frm, to) -> string

    Return a translation table (a string of 256 bytes long)
    suitable for use in string.translate.  The strings frm and to
    must be of the same length.

The maketrans line turns the dictionary into two separate strings suitable for input into maketrans:

>>> code = {'a': 'c', 'd': 'a', 'p': 'r', 'k': 'e', 'm': 'd'}
>>> code.items()
[('a', 'c'), ('p', 'r'), ('k', 'e'), ('m', 'd'), ('d', 'a')]
>>> zip(*code.items())
[('a', 'p', 'k', 'm', 'd'), ('c', 'r', 'e', 'd', 'a')]
>>> ["".join(x) for x in zip(*code.items())]
['apkmd', 'creda']
Sign up to request clarification or add additional context in comments.

Comments

6
"".join(code.get(k, k) for k in str)

would also work in your case.

code.get(k, k) returns code[k] if k is a valid key in code; if it isn't, it returns k itself.

Comments

6
>>> s = "pkm adp"
>>> code = {'a': 'c', 'd': 'a', 'p': 'r', 'k': 'e', 'm': 'd'}
>>> from string import maketrans
>>> s.translate(maketrans(''.join(code.keys()), ''.join(code.values())))
'red car'

Comments

0

though it would be tedious but a quick fix is str.replace("old", "new"). Here is the documentation for your help too http://www.tutorialspoint.com/python/string_replace.htm

Comments

0

Assuming you are using Python 2.x:

>>> from string import translate, maketrans
>>> data = "pkm adp"
>>> code = {'a': 'c', 'd': 'a', 'p': 'r', 'k': 'e', 'm': 'd'}
>>> table = maketrans(''.join(code.keys()), ''.join(code.values()))
>>> translate(data, table)
'red car'

Comments

0
>>>print ''.join([code.get(s,s) for s in str])
'red car'

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.