这是我的清单:
print(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"]
if matches in checklist:
print('found')
else:
print('not found')
print(checklist)
它总是返回“找不到”。但正如您在此处看到的一样。怎么解决呢?请指导
调用时您正在做的if matches in checklist:
是检查“清单”中是否存在变量“匹配”。
这意味着您正在查看列表 是否与清单中的列表matches
完全一样list
。
但这不是您真正想要的。您想查看清单中的任何条目中是否存在匹配项中的任何条目。
要对此进行检查,您需要遍历这些值。循环意味着您基本上会进行大量检查。
如果使用如下循环:
for item in checklist:
现在,您有一个循环,对于清单中的每个条目,该循环均采用名为“ item”的变量中该项目的值。
接下来,您需要认识到“匹配”列表也有多个条目,因此您还需要遍历它:
for match in matches:
将遍历match中的每个条目,并将值写入变量“ match”中。
现在您可以比较两者并进行检查:
if match in 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 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))
正如Mark Meyer所指出的那样,这将检查“ matches”中的字符串是否存在于“ checklist”中的字符串中,因此,如果您的单词是复合单词的一部分,它将返回true。
例如,如果清单中有“酸味”,它将认为“酸味”是有效的例子。如果您只想单独检查单词,我会做一些额外的检查。这使程序变得更加复杂并且不够完善,但是您会明白的:
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会导致超出范围的错误。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句