Transforming characters in Python
mir = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 't', 'u', 'v', 'x', 'y']
keq = ['.', 'c', 'z', 's', 'e', 'd', 'f', 'g', 'i', 'h', 'j', 'k', 'b', 'v', 'o', 'p', 'q', 'r', 't', 'u', 'x', ''\'', 'y']
I want to know if any of the characters in keq
are pressed in raw_input('write text: ')
to transform them in char开发者_JS百科acters that are shown in mir
Can someone help me do this... If you can write all the code it will help me so much
1. You could built a translation table for characters using the maketrans
and translate
methods, e.g.
>>> import string
>>> tb = string.maketrans('abc', '123')
>>> 'cyan banana'.translate(tb)
'3y1n 21n1n1'
2. You could concatenate all strings in an array using the ''.join
method, e.g.
>>> arr = ['a', 'b', 'c']
>>> ''.join(arr)
'abc'
These should be enough to solve your problem.
mir = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 't', 'u', 'v', 'x', 'y']
keq = ['.', 'c', 'z', 's', 'e', 'd', 'f', 'g', 'i', 'h', 'j', 'k', 'b', 'v', 'o', 'p', 'q', 'r', 't', 'u', 'x', '\\', 'y']
trans = dict(zip(mir,keq))
myStr = raw_input()
print ''.join([trans.has_key(ch) and trans[ch] or ch for ch in myStr])
Create a dictionary for the transformation ...
{
'a' : '.',
'b' : 'c',
...
then use map
to walk through your input, replacing keys with values.
精彩评论