Python 无法在项目列表中找到特定单词

Python 无法在项目列表中找到特定单词,python,list,Python,List,这是我的清单: 以下是我如何检查此列表中的字符串: 它总是返回而没有找到。但正如你所看到的,它现在就在那里。如何解决?请引导 调用清单中的if matches:时要做的事情是检查变量“matches”是否存在于“checklist”中 这意味着您正在查看列表是否与列表清单中的匹配项完全相同 但那不是你真正想要的。您想查看匹配项中的任何条目是否存在于检查表中的任何条目中 要检查这一点,您需要循环这些值。循环意味着你基本上要一个接一个地做很多检查 如果使用如下循环: for item in chec

这是我的清单:

以下是我如何检查此列表中的字符串:

它总是返回而没有找到。但正如你所看到的,它现在就在那里。如何解决?请引导


调用清单中的if matches:时要做的事情是检查变量“matches”是否存在于“checklist”中

这意味着您正在查看列表
是否与
列表
清单中的匹配项完全相同

但那不是你真正想要的。您想查看匹配项中的任何条目是否存在于检查表中的任何条目中

要检查这一点,您需要循环这些值。循环意味着你基本上要一个接一个地做很多检查

如果使用如下循环:

for item in checklist:
现在您有了一个循环,对于清单中的每个条目,该循环都假定该项的值位于名为“item”的变量中

接下来,您需要意识到您的“匹配项”列表也有多个条目,因此您还需要对其进行循环:

for match in matches:
将循环匹配中的每个条目,并将值写入变量“match”

现在,您可以比较这两者并进行检查:

if match in item:
如果“match”中的字符串存在于“item”中,则会触发此操作

所以有些代码

checklist=["A year on, AASU observes 'Black Day' to protest against CAA", u'Assam: Cargo reaches Lower Subansiri HE Project site despite protests', u'CAA will grant citizenship to 1.9 cr Hindu Bangladeshis in Assam: Akhil Gogoi', u'7 foods to increase estrogen levels in women', u'Meghalaya CM Conrad Sangma tests COVID-19 positive, has mild symptoms', u'AJYCP holds protest rally against CAA in Naharkatia', u"'Cash for job' scam: AMCH doctor's husband detained for interrogation", u"Meet 23-year-old Jojo Rajkumari, Manipur's first woman MMA fighter", u'Here are some food items to reduce heartburn and acid reflux']
matches = ["AASU", "Black Day"]
for item in checklist:
    for match in matches:
        if match in item:
            print("found")
将为原始列表中存在的“匹配”项的每个实例打印“已找到”

如果您想知道匹配,只需像这样修改它(从现在起我不会重写变量赋值)

如果您想知道在列表中的“何处”找到您的条目,您需要使用计数变量或使用索引

for i in range(len(checklist)):
    for j in range(len(matches)):
        if matches[j] in checklist[i]:
            print("Found! \""+matches[j]+"\" was found in \""+checklist[i]+"\", at position: "+str(i))
正如Mark Meyer所指出的,这将检查“匹配”中的字符串是否存在于“检查表”中的字符串中,因此如果您有一个单词是复合词的一部分,它将返回true

例如,如果你在检查表中有“酸”,它会认为“SododUp”是一个有效的例子。如果你只想单独检查单词,我会做一些额外的检查。这使程序变得更加复杂,并不完美,但你会明白:

checklist=["A year on, AASU observes 'Black Day' to protest against CAA", u'Assam: Cargo reaches Lower Subansiri HE Project site despite protests', u'CAA will grant citizenship to 1.9 cr Hindu Bangladeshis in Assam: Akhil Gogoi', u'7 foods to increase estrogen levels in women', u'Meghalaya CM Conrad Sangma tests COVID-19 positive, has mild symptoms', u'AJYCP holds protest rally against CAA in Naharkatia', u"'Cash for job' scam: AMCH doctor's husband detained for interrogation", u"Meet 23-year-old Jojo Rajkumari, Manipur's first woman MMA fighter", u'Here are some food items to reduce heartburn and acid reflux']
matches = ["AASU", "Black Day"]
for i in range(len(checklist)):
    for j in range(len(matches)):
        if matches[j] in checklist[i]:
            pos=checklist[i].find(matches[j])
            if pos!=0:
                if not checklist[i][pos-1].isalpha() and not checklist[i][pos+len(matches[j])].isalpha():
                    print("Found! \""+matches[j]+"\" was found in \""+checklist[i]+"\", at position: "+str(i))
            elif pos==0:
                if not checklist[i][pos+len(matches[j])].isalpha():
                    print("Found! \""+matches[j]+"\" was found in \""+checklist[i]+"\", at position: "+str(i))
这样做的目的是确保它不是另一个单词的一部分,在比赛前检查字母,比赛后检查字母,看看它是字母还是其他什么。如果它是一个字母,它就是另一个单词的一部分


检查“pos”==0以确保它不在字符串的开头,因为在前面查找字母1会导致超出范围的错误。

可能需要澄清OP是否只匹配整个单词。例如,如果<代码>匹配= [ [服务] ] < /代码>是否应该打印这个“打印”,因为<代码>观察< /代码>在句子中?这是一个很好的观察,我没有考虑。我可以修改代码来做到这一点,但这会使它变得更加复杂。我将添加它来说明。谢谢你的帮助。回答接受@MarkMeyer有一个很好的观点。我只想匹配匹配列表中的完整单词,但不能向上投票,因为没有任何代表认为这花费了我一点时间,但我添加了一个只匹配完整单词的版本。可能有bug,但在有限的测试中似乎对我有效。
for item in checklist:
    for match in matches:
        if match in item:
            print("Found! \""+match+"\" was found in \""+item+"\"!")
for i in range(len(checklist)):
    for j in range(len(matches)):
        if matches[j] in checklist[i]:
            print("Found! \""+matches[j]+"\" was found in \""+checklist[i]+"\", at position: "+str(i))
checklist=["A year on, AASU observes 'Black Day' to protest against CAA", u'Assam: Cargo reaches Lower Subansiri HE Project site despite protests', u'CAA will grant citizenship to 1.9 cr Hindu Bangladeshis in Assam: Akhil Gogoi', u'7 foods to increase estrogen levels in women', u'Meghalaya CM Conrad Sangma tests COVID-19 positive, has mild symptoms', u'AJYCP holds protest rally against CAA in Naharkatia', u"'Cash for job' scam: AMCH doctor's husband detained for interrogation", u"Meet 23-year-old Jojo Rajkumari, Manipur's first woman MMA fighter", u'Here are some food items to reduce heartburn and acid reflux']
matches = ["AASU", "Black Day"]
for i in range(len(checklist)):
    for j in range(len(matches)):
        if matches[j] in checklist[i]:
            pos=checklist[i].find(matches[j])
            if pos!=0:
                if not checklist[i][pos-1].isalpha() and not checklist[i][pos+len(matches[j])].isalpha():
                    print("Found! \""+matches[j]+"\" was found in \""+checklist[i]+"\", at position: "+str(i))
            elif pos==0:
                if not checklist[i][pos+len(matches[j])].isalpha():
                    print("Found! \""+matches[j]+"\" was found in \""+checklist[i]+"\", at position: "+str(i))