Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/356.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
检查列表中的单词是否出现在Python中的词典列表中_Python_List_Dictionary_For Loop - Fatal编程技术网

检查列表中的单词是否出现在Python中的词典列表中

检查列表中的单词是否出现在Python中的词典列表中,python,list,dictionary,for-loop,Python,List,Dictionary,For Loop,我有一个字典列表和五个单词列表。我想看看这些单子上的单词是否出现在字典里。因此,我使用了以下代码: import json with open("pbs_words_results_test.json") as json_file: data_words = json.load(json_file) 字典列表: [{"query": "love", "title": "How this repo

我有一个字典列表和五个单词列表。我想看看这些单子上的单词是否出现在字典里。因此,我使用了以下代码:

import json

with open("pbs_words_results_test.json") as json_file:
    data_words = json.load(json_file)
字典列表:

[{"query": "love", "title": "How this reporter learned to love nosy questions", "datetime": "Oct 21, 2020 08:27 PM EDT", "snippet": "Our October 2020 pick for the PBS NewsHour-New York Times book club is Paul Tough's \u201cHelping Children Succeed.\u201d", "url": "https://www.pbs.org/newshour/arts/how-this-reporter-learned-to-love-nosy-questions", "news_platform": "pbs"}, {"query": "love", "title": "Britney Spears shows love for #FreeBritney in court filing", "datetime": "Sep 04, 2020 12:06 AM EDT", "snippet": "Britney Spears is welcoming public scrutiny of the court conservatorship that has allowed her father to control her life and money for 12 years.", "url": "https://www.pbs.org/newshour/arts/britney-spears-shows-love-for-freebritney-in-court-filing", "news_platform": "pbs"}, {"query": "love", "title": "WATCH: Trump says QAnon conspiracists 'love our country'", "datetime": "Aug 19, 2020 09:54 PM EDT", "snippet": "... than 1,000 times in just 30 minutes. \"Holy Smokin Q,\" another tweeted. \"Our President was asked 2 questions about the Qanon movement TODAY!! We LOVE you President Trump.\" On Parler, a right-wing platform popular with some Trump supporters, one Qanon supporter posted a photo of Trump and a ...", "url": "https://www.pbs.org/newshour/politics/watch-live-trump-holds-white-house-news-conference-4", "news_platform": "pbs"},
for words in enjoyment:
    if words in data_words:
       data_words["emotions"] = "enjoyment"
词汇表:

with open("anger.json", "r") as read_file:
    anger = json.load(read_file)

with open("sadness.json", "r") as read_file:
    sadness = json.load(read_file)

with open("disgust.json", "r") as read_file:
    disgust = json.load(read_file)

with open("enjoyment.json", "r") as read_file:
    enjoyment = json.load(read_file)

with open("enjoyment.json", "r") as read_file:
    fear = json.load(read_file)
享受:

["love", "relief", "contentment", "amusement", "joy", "pride", "excitement", "peace", "satisfaction", "compassion", "liefde", "opluchting", "tevredenheid", "amusement", "vreugde", "trots", "opwinding", "vrede", "mededogen"]
然后使用此for循环遍历单词列表,如果单词在data_单词中,它将向字典添加一个额外的项:

[{"query": "love", "title": "How this reporter learned to love nosy questions", "datetime": "Oct 21, 2020 08:27 PM EDT", "snippet": "Our October 2020 pick for the PBS NewsHour-New York Times book club is Paul Tough's \u201cHelping Children Succeed.\u201d", "url": "https://www.pbs.org/newshour/arts/how-this-reporter-learned-to-love-nosy-questions", "news_platform": "pbs"}, {"query": "love", "title": "Britney Spears shows love for #FreeBritney in court filing", "datetime": "Sep 04, 2020 12:06 AM EDT", "snippet": "Britney Spears is welcoming public scrutiny of the court conservatorship that has allowed her father to control her life and money for 12 years.", "url": "https://www.pbs.org/newshour/arts/britney-spears-shows-love-for-freebritney-in-court-filing", "news_platform": "pbs"}, {"query": "love", "title": "WATCH: Trump says QAnon conspiracists 'love our country'", "datetime": "Aug 19, 2020 09:54 PM EDT", "snippet": "... than 1,000 times in just 30 minutes. \"Holy Smokin Q,\" another tweeted. \"Our President was asked 2 questions about the Qanon movement TODAY!! We LOVE you President Trump.\" On Parler, a right-wing platform popular with some Trump supporters, one Qanon supporter posted a photo of Trump and a ...", "url": "https://www.pbs.org/newshour/politics/watch-live-trump-holds-white-house-news-conference-4", "news_platform": "pbs"},
for words in enjoyment:
    if words in data_words:
       data_words["emotions"] = "enjoyment"

由于某些原因,它似乎无法在字典中找到单词。

因此,您的变量
data\u words
是一个字典列表

for words in enjoyment:
    if words in data_words: # Line 2: This condition will never be True.
       data_words["emotions"] = "enjoyment"
在代码段的第2行,您的条件尝试将包含字典的列表的内容与单个字符串相匹配

请注意,这将始终是错误的,因为字符串和字典是不同的(即使字典本身可能包含该字符串。代码不会自动为您搜索字典。中的
检查仅检查列表是否直接包含该项(本例中的
单词
变量)您正在搜索。)


然后,您需要做的是从列表中提取每一本词典,并在该词典的片段中搜索您的单词

# one dictionary at a time from the list of dicts
for dictionary in data_words: 
    # take one word from your list of words
    for word in enjoyment: 
        # word found in snippet.
        if word in dictionary['snippet']: 
            dictionary['emotions'] = "enjoyment" # Assuming that you want the emotions to be captured in the same dictionary. change as necessary. 
请注意,您也可以使用
any
功能使其更短

for dictionary in data_words:
    if any(word in dictionary['snippet'] for word in enjoyment):
        dictionary['emotions'] = "enjoyment"

你能举例说明一下
数据词
享受
实际上包含了什么吗?数据词[{“查询”:“爱”、“头衔”:“如何享受”[“爱”、“解脱”、“满足”",@roet——问题代码澄清应该放在问题帖子中,而不是放在评论中,以便潜在的回复者更容易找到。也就是说,不确定我是否理解数据词的结构。这是一个包含字典的列表吗?示例中的语法不正确。好的,谢谢你让我知道。这是一个由d组成的列表文章的索引。@roet太好了,你能编辑这个问题来添加这些片段吗?