如何从另一个列表中获取列表中某些单词的匹配项?
How to get matches for some words in a list from another list?
我有两个列表,我想将一个列表与另一个列表进行比较,并将所有接近的匹配项作为每个单词的输出。
例如:
a = ['apple','python','ice-cream']
b = ['aaple','aple','phython','icecream','cat','dog','cell']
所以当我通过列表 'a' 时,我需要得到 'aaple'、'aple'、'phython'、'icecream' 作为 [=27= 的输出].
我使用了 difflib.get_close_matches(word,pattern),但无法在其中一个输入中传递整个列表。
difflib.get_close_matches('apple',b)
OUTPUT:
['aaple','aple']
如何传递整个列表而不是一个单词?
以下代码将创建一个包含所有关闭词的列表:
import difflib
diff_words = []
for word in a:
diff_words += difflib.get_close_matches(word, b)
print(diff_words)
您可以使用嵌套列表理解,例如:
[close_match for word in a for close_match in difflib.get_close_matches(word, b)]
我有两个列表,我想将一个列表与另一个列表进行比较,并将所有接近的匹配项作为每个单词的输出。
例如:
a = ['apple','python','ice-cream']
b = ['aaple','aple','phython','icecream','cat','dog','cell']
所以当我通过列表 'a' 时,我需要得到 'aaple'、'aple'、'phython'、'icecream' 作为 [=27= 的输出].
我使用了 difflib.get_close_matches(word,pattern),但无法在其中一个输入中传递整个列表。
difflib.get_close_matches('apple',b)
OUTPUT:
['aaple','aple']
如何传递整个列表而不是一个单词?
以下代码将创建一个包含所有关闭词的列表:
import difflib
diff_words = []
for word in a:
diff_words += difflib.get_close_matches(word, b)
print(diff_words)
您可以使用嵌套列表理解,例如:
[close_match for word in a for close_match in difflib.get_close_matches(word, b)]