假设我有一个字符串列表
"Measles outbreak in the U.S worse than ever"
"MMR vaccination rates in California at all time low"
"I don't live in California"
和两个关键字列表
location = ['California', 'West Coast', 'Los Angeles']
disease = ['Measles', 'MMR', 'Pertussis']
如何从disease
和location
中选出包含至少一个关键字的字符串。
例如,应该挑出第二个字符串,而不是第一个或最后一个字符串。
答案 0 :(得分:2)
创建位置和疾病集,将子字符串拆分为单词,并查看两个集合中是否出现拆分字符串中的单词
location = {'California', 'West Coast', 'Los Angeles'}
disease = {'Measles', 'MMR', 'Pertussis'}
l = ['West Coast MMR',"Measles outbreak in the U.S worse than ever","MMR vaccination rates in California at all time low","I don't live in California"]
import re
r = re.compile("West Coast|Los Angeles|California")
for s in l:
if r.search(s) and any(word in disease for word in s.split()):
print(s)
for s in l:
if r.search(s) and disease.intersection(s.split()):
print(s)
如果字符串中至少有一个出现在两个集合中,则 if location.intersection(spl) and disease.intersection(spl):
将为True。 r.search(s)
从位置捕获两个字子串。
根据您的实际location
列表看起来混合的方式,set和re方法可能是最快的,首先检查集合然后使用或r.search(s)
编译正则表达式以匹配多字子串。
您可能还想使用字词边界,因此您不会与Californian
等匹配。:
r = re.compile("West Coast|Los Angeles|\bCalifornia\b")
根据可能出现的其他字词,您可能需要进行其他调整。在不知道您的实际数据集的情况下,无法给出明确或最佳的答案。
答案 1 :(得分:2)
import re
strings = [
"Measles outbreak in the U.S worse than ever.",
"MMR vaccination rates in Los Angeles at all time low.",
"I don't live in California.",
"The West Coast has many cases of Pertussis.",
"Do Californians even get Measles?",
]
kw_sets = [
["California", "West Coast", "Los Angeles"],
["Measles", "MMR", "Pertussis"],
]
patterns = ('|'.join(r'\b{}\b'.format(re.escape(kw)) for kw in kw_set)
for kw_set in kw_sets)
compiled_patterns = [re.compile(pattern) for pattern in patterns]
filterfunc = lambda s: all(cp.search(s) for cp in compiled_patterns)
filtered_strings = list(filter(filterfunc, strings))
print(*filtered_strings, sep='\n')
这是一个针对Python 3.x的正则表达式解决方案。
输出:
MMR vaccination rates in Los Angeles at all time low.
The West Coast has many cases of Pertussis.
答案 2 :(得分:0)
假设strings
是包含已定义字符串的列表,则
location = set(['California', 'West Coast', 'Los Angeles'])
disease = set(['Measles', 'MMR', 'Pertussis'])
res = [s for s in strings if ( set(s.split()) & location and set(s.split()) & disease ) ]
print res
将根据需要进行。请注意,set(s.split())
操作已完成两次,应予以考虑。
答案 3 :(得分:0)
正确处理多字关键字和两个以上的关键字设置:
strings = ("Measles outbreak in the U.S worse than ever",
"MMR vaccination rates in California at all time low",
"I don't live in California",
"MMR vaccination rates in California at all time",
"low West Coast Measles")
kwsets = (['California', 'West Coast', 'Los Angeles'],
['Measles', 'MMR', 'Pertussis'],
['low', 'prices', 'today'])
for string in strings:
if all(any(kw in string for kw in kws) for kws in kwsets):
print(string)
如果没有多字关键字,这也可行:
strings = ("Measles outbreak in the U.S worse than ever",
"MMR vaccination rates in California at all time low",
"I don't live in California",
"MMR vaccination rates in California at all time")
kwsets = ({'California', 'West Coast', 'Los Angeles'},
{'Measles', 'MMR', 'Pertussis'},
{'low', 'prices', 'today'})
for string in strings:
if all(map(set(string.split()).intersection, kwsets)):
print(string)