需要将字母更改为其他字母(Python 2.7) [英] Need to change letters to other letters (Python 2.7)
问题描述
我正在做一个学校的小项目,我们基本上需要把已经输入的字母改成另一个字母(从预制列表中选择),制作一个类似程序的代码机器.我在网上和 stackoverflow 上搜索过,但在上面找不到任何东西,也不知道从哪里开始.
I am making a school mini project, and we basically need to change letters that have been inputted into another letter (which is selected from a premade list), making a code machine like program. I have searched the web and stackoverflow, and cant find anything on it, and dont really know where to start.
我想让程序做的是,假设我输入了你好"这个词.程序会将单词改为loaaw",例如L为H,A为E,A为L,W为O.收到信件时.
What I want the programme to sort of do is this, say I input the word "hello". The program will change the word to "loaaw" for example, with L being H, A being E, A being L and W being O. These translations will be set before hand, in a big list, basically selecting which letter is sent back when a letter is sent in.
推荐答案
这样的事情?
orig = 'hello'
# table = {'h': 'L', 'e': 'O', 'l': 'A', 'o': 'W' }
# table_tr = dict( (ord(a), ord(b)) for a,b in table.items() )
table_tr = str.maketrans('helo', 'LOAW')
res = orig.translate(table_tr)
print(res)
(这是针对python3的;对于python2,你需要import string
并使用string.maketrans
)
(this is for python3; for python2 you need to import string
and use string.maketrans
)
这篇关于需要将字母更改为其他字母(Python 2.7)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!