python - 获取所有一个字符的字符串?
问题描述
如果我有一个“饿了”这个词并且我有一个类似的列表['asdfd', 'hingry', 'hungre', ' hangrr']
既然他们是一个角色,我怎么能得到'hingry'和'hungre'?我考虑过使用正则表达式,但我在 python 中使用它的经验并不多
解决方案
您可以使用itertools.zip_longest
和sum
:
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']
推荐阅读
- r - 纯 RDA 和双标图,删除贡献大于 10% 方差的值
- python - 登录网站 Flash.Com Python 1
- sql - HIVE 中的百分位数计算
- javascript - JQuery 缓动和 WebPack
- linux - 使用 Bash Perl 从命令输出中获取子字符串
- bash - 来自两个列表的多个命令
- swift - 保存核心数据属性时将索引从 0 移动到 1
- aws-lambda - 使用 Cognito 对 AWS-Amplify 中的 Lambda 函数进行查询和更改 GraphQL 进行身份验证
- python - PIP 不想安装更新或模块
- com - IOleCommandTarget.QueryStatus:支持 SaveAs 命令但未启用