Matching keywords in a dictionary to a list in Python

北战南征 提交于 2019-12-14 03:37:44

问题


The following dictionary gives the word and its value:

keywords = {'alone': 1, 'amazed': 10, 'amazing': 10, 'bad': 1, 'best': 10, 'better': 7, 'excellent': 10, 'excited': 10, 'excite': 10}

Following the dictionary are two tweets in a list in a list. For each tweet, we need to find which of the words from keywords are present in it.

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

The target is to find the sum of the keyword values found in each tweet line.

The code created needs to be a loop because there are more than 2 tweets. I do not understand how I should execute this process.

Appreciate your insight!


回答1:


Try this:

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



回答2:


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

If you don't like the values.append(0) you can change it to new = 0 and the values[-1] to tmp. You will also need to add at the end of the first loop values.append(tmp).
Also, remember that x += y can be read as x = x + y.

If you want to have a total score, you can:

# ^ 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)

Or if you want small codes:

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]

Your choice.




回答3:


First we need to assign a variable for the value and set this to zero, then for each tweet and for every word inside this tweet we use the function dict.get() to get the corresponding value of word (if the word isn't in keywords it returns 0).

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


来源:https://stackoverflow.com/questions/47315857/matching-keywords-in-a-dictionary-to-a-list-in-python

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!