【问题标题】:Matching keywords in a dictionary to a list in Python将字典中的关键字与 Python 中的列表匹配
【发布时间】:2018-04-29 04:14:39
【问题描述】:

下面的字典给出了这个词和它的值:

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

字典后面是列表中的两条推文。 对于每条推文,我们需要找出其中有哪些来自 keywords 的词。

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 条推文。 我不明白我应该如何执行这个过程。

欣赏您的洞察力!

【问题讨论】:

  • 你试过什么?对您遇到的问题有任何疑问吗?

标签: python string list dictionary matching


【解决方案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']]
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

【讨论】:

  • 那将是投票勾结。不要那样做。
  • 此答案检查推文中的关键字。这是低效的,在某些情况下是不正确的。
  • 在一条推文中多次重复某个单词,它不会起作用。您还隐藏了一个内置函数,这是一个坏主意。您还进行了不必要的列表查找。
  • 不,你不是。
  • 不,效率不高。您只需要对推文和推文词进行一次传递。通过使用.count,您最终会进行多次遍。
【解决方案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

如果您不喜欢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]

你的选择。

【讨论】:

    【解决方案3】:

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

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

    【讨论】:

    • 这个答案是不必要的低效。
    • @miradulo 请告诉我什么是低效?
    • 首先,if word in keywords.keys() 是一种反模式。首先,不需要调用.keys,只需x in my_dict 即可检查成员资格。更糟糕的是,在 Python 2 上,它会创建一个 键列表,然后进行 O(n) 查找。在 Python 3 上,它只是多余的。相反,只需使用字典,即value += keywords.get(word, 0)
    • 谢谢!没有太多的字典经验,今天学到了新东西:)
    猜你喜欢
    • 2020-05-18
    • 1970-01-01
    • 1970-01-01
    • 2021-11-17
    • 2022-10-14
    • 1970-01-01
    • 2021-01-10
    • 2022-11-18
    • 2022-08-19
    相关资源
    最近更新 更多