【发布时间】:2015-11-24 17:23:37
【问题描述】:
我正在尝试创建一个程序来执行非常简单的加密/压缩。
基本上,程序以句子的形式请求输入,该句子被分成单词,例如
this is the gamma
然后我有两个列表:
mylist1 = ("alpha","beta","gamma","delta")
mylist2 = ("1","2","3","4")
每个单词都与包含多个单词的列表进行匹配。如果单词出现在列表中,那么它应该用另一个列表中的相应数字替换单词。输出应该是:
this is the 3
这是我目前的代码:
text = input("type your sentence \n")
words = text.split(" ")
mylist1 = ("alpha","beta","gamma","delta")
mylist2 = ("1","2","3","4")
#I was looking at zipping the lists together but wasn't sure I was on the right track
#ziplist = zip(mylist1, mylist2)
for word in words:
if word in mylist1:
text = text.replace(word,mylist2[])
print (text)
This question that I was looking into yesterday 展示了如何使用字典执行此操作,但我在尝试将文本文件再次从数字转换回单词时遇到了问题。 (我交换了键和值。我很确定我不应该这样做)
任何输入都会很棒。
【问题讨论】: