Python 如果在用户输入中找到键,则从字典对象打印多个键和值 *********编辑*********
正如一些人所评论的,这是我的代码的一个更完整的版本。我需要它做的是打印在user_tweet属性中找到的任何键和相应的值,该属性是一个不超过160个字符的字符串。不幸的是,我所能做的就是打印用户_tweet中最后出现的一个键Python 如果在用户输入中找到键,则从字典对象打印多个键和值 *********编辑*********,python,dictionary,Python,Dictionary,正如一些人所评论的,这是我的代码的一个更完整的版本。我需要它做的是打印在user_tweet属性中找到的任何键和相应的值,该属性是一个不超过160个字符的字符串。不幸的是,我所能做的就是打印用户_tweet中最后出现的一个键 #Dictionary item abb_dict = { 'lol': 'laughing out loud', 'bfn': 'bye for now', 'cuz': 'because', 'gtg': 'got to go', 'brb': 'be right b
#Dictionary item
abb_dict = {
'lol': 'laughing out loud',
'bfn': 'bye for now',
'cuz': 'because',
'gtg': 'got to go',
'brb': 'be right back',
'nvm': 'nevermind',
'bff': 'best friends for ever',
'rofl': 'rolling on the floor laughing',
'omg': 'oh my god',}
user_choice = raw_input() ###Determines if user quits program
t_round = 0 #determines how many tweets have been decoded for exit message
while True:
if user_choice == 'Q' or user_choice =='q': #quits program
break
#INPUT INTO user_tweet: omg shut up i gtg
user_tweet = raw_input('Type a Tweet 160 Character or less').lower()
if len(user_tweet) <= 160:
for x in abb_dict:
if x in user_tweet:
print x , abb_dict[x]
#OUTPUT: gtg got to go
#字典项
abb_dict={
“大笑”:“大声大笑”,
“bfn”:“再见”,
‘因为’:‘因为’,
“gtg”:“必须走了”,
“brb”:“马上回来”,
'nvm':'nevermind',
“bff”:“永远最好的朋友”,
“rofl”:“在地板上打滚大笑”,
“天哪:'我的天哪',}
user_choice=raw_input()####确定用户是否退出程序
t_round=0#确定为退出消息解码的推文数量
尽管如此:
如果用户选择=='Q'或用户选择=='Q':#退出程序
打破
#输入用户_tweet:omg闭嘴i gtg
user_tweet=raw_input('Type a tweet 160个字符或更少')。lower()
如果len(user_tweet)您的第一个解决方案是正确的。它应该输出每一个匹配项,而不仅仅是最后一个。我的Python测试:
abbrev_dict = {}
abbrev_dict['omg'] = "Oh My God!"
abbrev_dict['gtg'] = "Got to go"
input = "omg shut up I gtg"
for x in abbrev_dict:
if x in input:
print(x + ":" + abbrev_dict[x])
# Output:
# omg:Oh My God!
# gtg:Got to go
我猜abbrev_dict
的键不是小写的(例如OMG
而不是OMG
),因此与输入不匹配(强制为小写)。尝试将键小写:
abbrev_dict = {key.lower():value for key,value in abbrev_dict.items()}
你的第一个解决方案是正确的。它应该输出每一个匹配项,而不仅仅是最后一个。我的Python测试:
abbrev_dict = {}
abbrev_dict['omg'] = "Oh My God!"
abbrev_dict['gtg'] = "Got to go"
input = "omg shut up I gtg"
for x in abbrev_dict:
if x in input:
print(x + ":" + abbrev_dict[x])
# Output:
# omg:Oh My God!
# gtg:Got to go
我猜abbrev_dict
的键不是小写的(例如OMG
而不是OMG
),因此与输入不匹配(强制为小写)。尝试将键小写:
abbrev_dict = {key.lower():value for key,value in abbrev_dict.items()}
用户输入
已损坏。调用lower
时忘记了括号。它实际上被设置为函数,而不是下框输入字符串。用户输入被破坏。调用lower
时忘记了括号。它实际上被设置为函数,而不是下格的输入字符串。您的新代码在Python 2.6.6上运行良好,因此您只打印最后一个缩写的问题有点神秘。但是,可以对程序的逻辑进行一些改进
首先,您应该测试用户是否希望在主循环内退出。但是你有一个循环之外的东西:
user_choice = raw_input() ###Determines if user quits program
if user_choice == 'Q' or user_choice =='q': #quits program
break
这是在循环中:
user_choice = raw_input() ###Determines if user quits program
if user_choice == 'Q' or user_choice =='q': #quits program
break
但是,由于您只在循环外部设置了user\u choice
,因此测试将始终给出相同的结果,因此,您要么在第一次进入时立即中断,而循环不处理推文,要么陷入无限循环,必须按Ctrl-C才能摆脱
您的主处理循环可能会发现错误匹配:它将打印嵌入较长单词中的键,这可能不是您想要的。例如,如果user\u tweet
包含“按摩浴缸”,那么它将与“cuz”匹配
而且,主处理循环效率低下
for x in abb_dict:
if x in user_tweet:
对每个tweet重复整个abb_dict
,然后if in
测试必须通过tweet线性扫描,从abb_dict
查找当前键的匹配项。现在,如果user_tweet中的x相当快,因为它以C速度运行,这意味着它比以Python速度扫描要快,但它仍然效率低下
for x in abb_dict:
if x in user_tweet:
此外,您的代码按照缩写词在字典中的存储顺序打印缩写词,这可能与在字典定义中列出缩写词的顺序不同(由于Python字典的工作方式)
一个更好的策略是将tweet分成一个单词列表,然后测试每个单词是否在abb_dict
中。这样,您只需线性扫描tweet两次,一次将其拆分为列表,一次根据字典测试列表中的每个单词。测试一个键是否在字典中是非常快速有效的,因为它不会扫描整个字典寻找匹配项。此外,通过这种策略,您可以按照缩写词在tweet中出现的顺序获得它们的列表
以下是代码的改进版本:
abb_dict = {
'lol': 'laughing out loud',
'bfn': 'bye for now',
'cuz': 'because',
'gtg': 'got to go',
'brb': 'be right back',
'nvm': 'nevermind',
'bff': 'best friends for ever',
'rofl': 'rolling on the floor laughing',
'omg': 'oh my god',
}
#determines how many tweets have been decoded for exit message
t_round = 0
while True:
user_tweet = raw_input('Type a Tweet of 160 characters or less, or q to quit: ')
if user_tweet in "Qq":
break
if len(user_tweet) > 160:
continue
for word in user_tweet.lower().split():
if word in abb_dict:
print '%s: %r' % (word, abb_dict[word])
t_round += 1
print t_round, 'tweets processed.'
测试
Type a Tweet of 160 characters or less, or q to quit: omg shut up i gtg
omg: 'oh my god'
gtg: 'got to go'
Type a Tweet of 160 characters or less, or q to quit: LOL dude brb I gtG
lol: 'laughing out loud'
brb: 'be right back'
gtg: 'got to go'
Type a Tweet of 160 characters or less, or q to quit: q
2 tweets processed.
Type a Tweet of 160 characters or less, or q to quit: lol dude brb i GTG LOL brb gtg nvm
lol: 'laughing out loud'
brb: 'be right back'
gtg: 'got to go'
nvm: 'nevermind'
Type a Tweet of 160 characters or less, or q to quit: Q
1 tweets processed.
Type a Tweet of 160 characters or less, or q to quit: lol dude brb i GTG LOL brb gtg nvm
laughing out loud dude be right back i got to go laughing out loud be right back got to go nevermind
Type a Tweet of 160 characters or less, or q to quit: q
1 tweets processed.
请注意,此代码处理大小写混合以及单词之间的大量空白,但不处理附加了大量标点符号的缩写。同样,如果同一个缩写在tweet中出现多次,它将被打印多次。如果不需要,您可以使用集合
来存储匹配项,并且仅当匹配项不在集合
中时才打印匹配项。像这样:
matches = set()
for word in user_tweet.lower().split():
if word in abb_dict and word not in matches:
print '%s: %r' % (word, abb_dict[word])
matches.add(word)
测试
Type a Tweet of 160 characters or less, or q to quit: omg shut up i gtg
omg: 'oh my god'
gtg: 'got to go'
Type a Tweet of 160 characters or less, or q to quit: LOL dude brb I gtG
lol: 'laughing out loud'
brb: 'be right back'
gtg: 'got to go'
Type a Tweet of 160 characters or less, or q to quit: q
2 tweets processed.
Type a Tweet of 160 characters or less, or q to quit: lol dude brb i GTG LOL brb gtg nvm
lol: 'laughing out loud'
brb: 'be right back'
gtg: 'got to go'
nvm: 'nevermind'
Type a Tweet of 160 characters or less, or q to quit: Q
1 tweets processed.
Type a Tweet of 160 characters or less, or q to quit: lol dude brb i GTG LOL brb gtg nvm
laughing out loud dude be right back i got to go laughing out loud be right back got to go nevermind
Type a Tweet of 160 characters or less, or q to quit: q
1 tweets processed.
或者,您可以将matches=set()
移动到之前,而True
保留所有tweet的匹配项,以便只打印每个tweet中的新缩写
只要对代码稍作修改,我们就可以通过扩展来替换缩写:
abb_dict = {
'lol': 'laughing out loud',
'bfn': 'bye for now',
'cuz': 'because',
'gtg': 'got to go',
'brb': 'be right back',
'nvm': 'nevermind',
'bff': 'best friends for ever',
'rofl': 'rolling on the floor laughing',
'omg': 'oh my god',
}
#determines how many tweets have been decoded for exit message
t_round = 0
while True:
user_tweet = raw_input('Type a Tweet of 160 characters or less, or q to quit: ')
if user_tweet in "Qq":
break
if len(user_tweet) > 160:
continue
words = []
for word in user_tweet.split():
word = abb_dict.get(word.lower(), word)
words.append(word)
print ' '.join(words)
t_round += 1
print t_round, 'tweets processed.'
测试
Type a Tweet of 160 characters or less, or q to quit: omg shut up i gtg
omg: 'oh my god'
gtg: 'got to go'
Type a Tweet of 160 characters or less, or q to quit: LOL dude brb I gtG
lol: 'laughing out loud'
brb: 'be right back'
gtg: 'got to go'
Type a Tweet of 160 characters or less, or q to quit: q
2 tweets processed.
Type a Tweet of 160 characters or less, or q to quit: lol dude brb i GTG LOL brb gtg nvm
lol: 'laughing out loud'
brb: 'be right back'
gtg: 'got to go'
nvm: 'nevermind'
Type a Tweet of 160 characters or less, or q to quit: Q
1 tweets processed.
Type a Tweet of 160 characters or less, or q to quit: lol dude brb i GTG LOL brb gtg nvm
laughing out loud dude be right back i got to go laughing out loud be right back got to go nevermind
Type a Tweet of 160 characters or less, or q to quit: q
1 tweets processed.
通过使用列表理解,可以压缩该中心循环(并稍微提高效率):
while True:
user_tweet = raw_input('Type a Tweet of 160 characters or less, or q to quit: ')
if user_tweet in "Qq":
break
if len(user_tweet) > 160:
continue
print ' '.join([abb_dict.get(word.lower(), word)
for word in user_tweet.split()])
t_round += 1
现在看看是否可以修改这些代码,以便它们处理标点符号。:) 您的新代码在Python 2.6.6上运行良好,因此您只打印最后一个缩写的问题有点神秘。但是,可以对程序的逻辑进行一些改进
首先,您应该测试用户是否希望在主循环内退出。但是你有一个循环之外的东西:
user_choice = raw_input() ###Determines if user quits program
if user_choice == 'Q' or user_choice =='q': #quits program
break
这是在循环中:
user_choice = raw_input() ###Determines if user quits program
if user_choice == 'Q' or user_choice =='q': #quits program
break
但是,由于您只在循环外部设置了user\u choice
,测试将始终给出相同的结果,因此,您要么在第一次进入时立即中断,而循环,不处理推文,要么陷入无限循环,无法处理推文