将字典中的关键字匹配到Python中的列表

时间:2017-11-15 19:36:17

标签: python string list dictionary matching

以下字典给出了单词及其值:

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']] 

目标是找到每条推文中找到的关键字值总和

创建的代码需要成为循环,因为有超过2条推文。 我不明白我应该如何执行这个过程。

欣赏你的见解!

3 个答案:

答案 0 :(得分:0)

试试这个:

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

答案 1 :(得分:-1)

首先我们需要为值分配一个变量并将其设置为零,然后对于每个推文和本推文中的每个单词,我们使用函数dict.get()来获取相应的单词值(如果单词不在关键字中返回0)。

value = 0
for tweet in tweets:
    for word in tweet:
        value += keywords.get(word,0)

答案 2 :(得分:-1)

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']]

values = []              # Here we will store the score of each tweat like an item

for tweet in tweets:     # We iterate over each tweet
    values.append(0)     # We add a new item to the list values, we'll change this number later.
    for word in tweet:   # We iterate over each word in the tweet
        values[-1] += keywords.get(word, 0) # Using .get() we get the value of a word if it's inside keyword, if not, we get a default value: 0, instead of an KeyError. 
print(values) # Obviously, print the values in console

如果您不喜欢values.append(0),可以将其更改为new = 0,将values[-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]

您的选择。