我有两个字符串列表:
grids = ['north', 'eaSt', 'West','noRtheast', 'soUth']
links = ['north-northeast', 'north-south']
我想检查一下是什么grids
有在links
。所以我为此写了一个程序:
import re
grids = ['north', 'eaSt', 'West','noRtheast', 'soUth']
links = ['north-northeast', 'north-south']
for search in grids:
for text in links:
result = re.findall('\\b' + search + '\\b', text, flags=re.IGNORECASE)
print(result)
OUTPUT:
['north']
['north']
[]
[]
[]
[]
['northeast']
[]
[]
['south']
我几乎得到了输出,但不明白为什么我会在输出中得到这些空白,那么我可以得到更简单、干净的替代方案吗?
可能不需要正则表达式。只需检查该字符串是否存在。
grids = ['north', 'eaSt', 'West','noRtheast', 'soUth']
links = ['north-northeast', 'north-south']
for l in links:
print(f'{l} contains {[x for x in grids if x.lower() in l.lower().split("-")]}')
Output
north-northeast contains ['north', 'noRtheast']
north-south contains ['north', 'soUth']
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)