在Python中将字典中的关键字与列表匹配
以下词典给出了该词及其值:在Python中将字典中的关键字与列表匹配,python,string,list,dictionary,matching,Python,String,List,Dictionary,Matching,以下词典给出了该词及其值: keywords = {'alone': 1, 'amazed': 10, 'amazing': 10, 'bad': 1, 'best': 10, 'better': 7, 'excellent': 10, 'excited': 10, 'excite': 10} 字典后面是列表中的两条tweet。 对于每条tweet,我们需要找到其中存在的关键字中的哪些词 tweets = [['work', 'needs', 'to', 'fly', 'by', '', "i
keywords = {'alone': 1, 'amazed': 10, 'amazing': 10, 'bad': 1, 'best': 10, 'better': 7, 'excellent': 10, 'excited': 10, 'excite': 10}
字典后面是列表中的两条tweet。
对于每条tweet,我们需要找到其中存在的关键字中的哪些词
tweets = [['work', 'needs', 'to', 'fly', 'by', '', "i'm", 'so', 'excited', 'to', 'see', 'spy', 'kids', '4', 'with', 'then', 'love', 'of', 'my', 'life', '', 'arreic'], ['today', 'is', 'going', 'to', 'be', 'the', 'greatest', 'day', 'of', 'my', 'life', 'hired', 'to', 'take', 'pictures', 'at', 'my', 'best', "friend's", 'gparents', '50th', 'anniversary', '60', 'old', 'people', 'woo']]
目标是找到在每条tweet行中找到的关键字值的总和
创建的代码需要是一个循环,因为有两条以上的tweet。
我不明白我应该如何执行这个过程
感谢你的洞察力 试试这个:
keywords = {'alone': 1, 'amazed': 10, 'amazing': 10, 'bad': 1, 'best': 10, 'better': 7, 'excellent': 10, 'excited': 10, 'excite': 10}
tweets = [['work', 'needs', 'to', 'fly', 'by', '', "i'm", 'so', 'excited', 'to', 'see', 'spy', 'kids', '4', 'with', 'then', 'love', 'of', 'my', 'life', '', 'arreic'], ['today', 'is', 'going', 'to', 'be', 'the', 'greatest', 'day', 'of', 'my', 'life', 'hired', 'to', 'take', 'pictures', 'at', 'my', 'best', "friend's", 'gparents', '50th', 'anniversary', '60', 'old', 'people', 'woo']]
total = 0
for i in keywords:
for j in tweets:
if i in j:
occourance = j.count(i)
print('keyword=', i)
total += keywords[i]*occourance
print('sum is: ', total)
output:
keyword= best
keyword= excited
sum is: 20
首先,我们需要为该值分配一个变量并将其设置为零,然后对于每条tweet以及该tweet中的每个单词,我们使用函数
dict.get()
获取单词的相应值(如果单词不在关键字中,则返回0)
如果您不喜欢值。附加(0)
可以将其更改为new=0
,将值[-1]
更改为tmp
。您还需要在第一个循环的末尾添加values.append(tmp)
另外,请记住,
x+=y
可以理解为x=x+y
如果您想获得总分,您可以:
# ^ Use the code above ^
total_value = sum(values) # It sum all the items of values
print(total_value)
# Or total new code.
total_score = 0
for tweet in tweets:
for word in tweet:
total_score += keywords.get(word, 0)
print(total_score)
或者,如果您需要小代码:
total_value = sum([keywords.get(word,0) for tweet in tweets for word in tweet])
value = [sum([keywords.get(word, 0) for word in tweet]) for tweet in tweets]
您的选择。您尝试过什么?您是否有任何与您遇到的问题相关的问题?此答案毫无必要地低效。@miradulo请告诉我什么是低效的?首先,
如果word in keys()
是反模式。首先,不需要调用.keys
,只需在我的字典中调用x即可检查成员资格。更糟糕的是,在Python2上,它创建一个键列表,然后执行O(n)查找。在Python3上,它只是多余的。相反,只需使用字典,即value+=keywords.get(word,0)
谢谢!没有那么多字典方面的经验,今天学到了一些新东西:)那就是投票串通。不要这样做。这个答案会检查推文中的关键词。这是低效的,在某些情况下是不正确的。在tweet中重复某个单词多次,它将不起作用。你也在跟踪一个内置的,这是个坏主意。你也在做一个不必要的列表查找。不,你没有。不,它没有效率。你只需要在tweets和tweet单词上做一次简单的传递。通过使用.count
,您将完成多个过程。
# ^ Use the code above ^
total_value = sum(values) # It sum all the items of values
print(total_value)
# Or total new code.
total_score = 0
for tweet in tweets:
for word in tweet:
total_score += keywords.get(word, 0)
print(total_score)
total_value = sum([keywords.get(word,0) for tweet in tweets for word in tweet])
value = [sum([keywords.get(word, 0) for word in tweet]) for tweet in tweets]