【发布时间】:2023-02-16 01:28:05
【问题描述】:
Example:
myList = []
text = ["salmonella in black pepper from brazil", "aflatoxins in fish from germany", "pseudomonas in meat from italy"]
findmatches = re.compile(r"\b" +
r"\b|\b".join(re.escape(hazard) for hazard in hazards_set) +
r"\b")
for i in text:
for possible_match in set(findmatches.findall(i)):
if possible_match in hazards_set:
myList.append(possible_match)
myList.append("")
print(myList)
This is what I get:
['salmonella', '', 'aflatoxins', '', '']
This is what I would like to get:
['salmonella','aflatoxins', '']
since "pseudomonas" is not in the set hazards_set.
How can I solve the problem?
标签: python
hazards_set?