首页 > 解决方案 > 获取所有一个字符的字符串?

问题描述

如果我有一个“饿了”这个词并且我有一个类似的列表['asdfd', 'hingry', 'hungre', ' hangrr']

既然他们是一个角色,我怎么能得到'hingry'和'hungre'?我考虑过使用正则表达式,但我在 python 中使用它的经验并不多

标签: pythonregex

解决方案


您可以使用itertools.zip_longestsum

from itertools import zip_longest as _zip
d = ['asdfd', 'hingry', 'hungre', ' hangrr']
word = 'hungry' 
results = [i for i in d if sum(a == b for a, b in _zip(i, word)) >= len(word)-1]

输出:

['hingry', 'hungre']

推荐阅读