请求一些帮助。此函数将缩写词替换为标准拼写。我试图用列表理解来代替第3-6行。我试过这个:
words[i] = [key for i, word in enumerate(words) for key, value in abbrevs.items()
if word == abbrevs[key]]
def text_deciphered(text_message, abbrevs):
words = text_message.split()
for i, word in enumerate(words):
for key in abbrevs.keys():
if word == abbrevs[key]:
words[i] = key
words = ' '.join(words)
return words
text_message = "Hey, wat r our plans for tn"
abbrevs = {"what": "wat", "are": "r", "tonight": "tn"}
print(text_deciphered(text_message, abbrevs))
但它只是以缩写形式返回密钥——今晚是什么
首先,字典背后的理念是基于键查找值,而不是相反。这保证了即时查找,而不是像您所做的那样丢弃所有条目并逐个查找。从反转dict:abbrevs = {v: k for k, v in abbrevs.items()}
开始,或者在继续之前从一开始就正确构建它。
在调整之后,这可以是使用列表理解和dict.get(element, element)
的简单的一行。如果element
不在字典中,则回退值为element
本身:
>>> text_message = "Hey, wat r our plans for tn"
>>> abbrevs = {"wat": "what", "r": "are", "tn": "tonight"}
>>> " ".join([abbrevs.get(x, x) for x in text_message.split()])
'Hey, what are our plans for tonight'
所以你的功能是:
def text_deciphered(text_message, abbrevs):
return " ".join([abbrevs.get(x, x) for x in text_message.split()])
请注意,split
相当于s+
,因此您可能会无意中压缩空白。我更喜欢使用带有lambda的re.sub
来应用于每个单词:
>>> import re
>>> text_message = "Hey, wat r our plans for tn"
>>> abbrevs = {"wat": "what", "r": "are", "tn": "tonight"}
>>> re.sub(r"(w+)", lambda x: abbrevs.get(x.group(), x.group()), text_message)
'Hey, what are our plans for tonight'