在 python 中的字符串列表中找到确切的单词列表?
find the exact list of words in list of strings in python?
我有两个字符串列表:
grids = ['north', 'eaSt', 'West','noRtheast', 'soUth']
links = ['north-northeast', 'north-south']
我想检查 links
中有什么 grids
。所以我为此写了一个程序:
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)
输出:
['north']
['north']
[]
[]
[]
[]
['northeast']
[]
[]
['south']
我几乎得到了输出,但不明白为什么我在输出中得到那些空白,所以我可以得到更简单干净的替代品吗?
当常规 Exp 找不到任何内容时,您的代码打印结果也是如此。
您可以添加一些 if
检查数组是否为空
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)
if len(result):
print(result)
可能不需要为此使用正则表达式。只需检查字符串是否存在。
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("-")]}')
输出
north-northeast contains ['north', 'noRtheast']
north-south contains ['north', 'soUth']
我不知道图书馆“re”,但我想我可以像这样解决你的问题:
grids = ['north', 'eaSt', 'West','noRtheast', 'soUth']
links = ['north-northeast', 'north-south']
for search in grids:
for text in links:
if search.lower() in text.lower():
print('find '+search+' in '+text+' !')
</pre>
字符串上的方法 .lower() 会将所有基于大小写的字符小写,如果字符串 search
在字符串 text
.
好吧,我有误会,你可以在比较之前拆分链:
grids = ['north', 'eaSt', 'West','noRtheast', 'soUth']
links = ['north-northeast', 'north-south']
for search in grids:
for text in links:
for text_split in text.split('-'):
if search.lower() == text_split.lower():
print('find '+search+' in '+text+' !')</pre>
输出:
find north in north-northeast !
find north in north-south !
find noRtheast in north-northeast !
find soUth in north-south !</pre>
我有两个字符串列表:
grids = ['north', 'eaSt', 'West','noRtheast', 'soUth']
links = ['north-northeast', 'north-south']
我想检查 links
中有什么 grids
。所以我为此写了一个程序:
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)
输出:
['north']
['north']
[]
[]
[]
[]
['northeast']
[]
[]
['south']
我几乎得到了输出,但不明白为什么我在输出中得到那些空白,所以我可以得到更简单干净的替代品吗?
当常规 Exp 找不到任何内容时,您的代码打印结果也是如此。
您可以添加一些 if
检查数组是否为空
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)
if len(result):
print(result)
可能不需要为此使用正则表达式。只需检查字符串是否存在。
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("-")]}')
输出
north-northeast contains ['north', 'noRtheast']
north-south contains ['north', 'soUth']
我不知道图书馆“re”,但我想我可以像这样解决你的问题:
grids = ['north', 'eaSt', 'West','noRtheast', 'soUth'] links = ['north-northeast', 'north-south'] for search in grids: for text in links: if search.lower() in text.lower(): print('find '+search+' in '+text+' !') </pre>
字符串上的方法 .lower() 会将所有基于大小写的字符小写,如果字符串
search
在字符串text
.
好吧,我有误会,你可以在比较之前拆分链:
grids = ['north', 'eaSt', 'West','noRtheast', 'soUth'] links = ['north-northeast', 'north-south'] for search in grids: for text in links: for text_split in text.split('-'): if search.lower() == text_split.lower(): print('find '+search+' in '+text+' !')</pre>
输出:
find north in north-northeast ! find north in north-south ! find noRtheast in north-northeast ! find soUth in north-south !</pre>