python - 无法在项目列表中找到特定单词
问题描述
这是我的清单:
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)
它总是返回 Not Found。但正如你所看到的那样。如何解决?请指导
解决方案
当你打电话时,你正在做的事情if matches in checklist:
是检查变量“matches”是否存在于“checklist”中。
这意味着您正在查看列表是否与清单 matches
中的完全相同list
。
但这不是你真正想要的。您想查看match 中的任何条目是否存在于 checklist 中的任何条目中。
要检查这一点,您需要遍历这些值。循环意味着您基本上一个接一个地进行大量检查。
如果您使用如下循环:
for item in checklist:
您现在有一个循环,对于清单中的每个条目,都假定该条目在名为“item”的变量中的值。
接下来你需要意识到你的“匹配”列表也有多个条目,所以你也需要遍历它:
for match in matches:
将遍历 match 中的每个条目并将值写入变量“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 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 所指出的,这会检查“匹配”中的字符串是否存在于“清单”中的字符串中,因此如果您有一个单词是复合词的一部分,它将返回 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 会导致超出范围错误。
推荐阅读
- go - 等效结构上字段的接口转换
- reactjs - React-native 中的帮助屏幕覆盖
- java - 如何在java中对字符串进行排序,顺序应首先显示大写字符串
- regex - 如何使用 Nifi 转换 csv 文件中的列?
- c# - (Unity) 使用 BuildPipeline 构建时启用 App Bundle (Google Play) 选项?
- javafx - Gluon 应用程序在 MousePressed 快速单击或双击、三次单击时出现问题
- react-native - 任务应用程序:bundleDebugJsAndAssets 在 gitlab-ci 构建上失败
- gremlin - AWS-Neptune 中使用 Gremlin 的正则表达式功能
- ruby - 如何使用 rest-client 将 cURL multipart/form-data POST 请求转换为 ruby 请求?
- javascript - 我应该在 GraphQL Scalar 中抛出一般错误吗?