【发布时间】:2019-04-11 09:46:07
【问题描述】:
我是编程新手,我正在用 Python 编写一个小程序,以使用“字典”来缩写句子中的单词。这个想法是我会写出一个句子,并且作为键包含的任何单词都将返回其缩写值。
我正在使用split() 方法来帮助实现这一目标。问题是当我有一个部分类似于另一个的密钥时,例如密钥“帐户”将打印为“acct”,但是当我希望将不同的密钥“帐户接管”打印为“ato”时,它将不起作用。我假设 split() 现在对我不利。我查看了字符串方法和字典方法,但无法提出解决方案。有任何想法吗?非常感谢任何帮助。
notes = input("Notes: ")
def simple_library(notes):
abbreviate = notes.lower().split(" ")
abbreviate_sentence = {
"account": "acct",
"account takeover": "ato"
}
output = ""
for words in abbreviate:
output += abbreviate_sentence.get(words, words) + " "
return output.upper()
print(simple_library(notes))
注意事项:帐号、帐号接管
帐户,帐户接管
我希望它打印为 账户,税务局
【问题讨论】:
标签: python dictionary key