python - 制作需要删除列表元素的循环脚本
问题描述
Python问题
import fnmatch
list_1 = ['family', 'brother', 'snake', 'famfor']
list_2 = ['a', 'f', 'f', 'm', 'i', 'l', 'y']
match = fnmatch.filter(list_1, 'fa????')
print match
这会给我
>> ['family', 'famfor']
我怎样才能在这个查询中只得到家人?通过检查 list_2 的有效字母。
解决方案
您可以先转换list_2
为集合以进行高效查找,然后使用带有条件的列表推导作为过滤器:
set_2 = set(list_2)
[w for w in list_1 if all(c in set_2 for c in w)]
这将返回:
['family']
推荐阅读
- c - 如何在C中的二维数组中的列下打印值?
- xcode - protobuf:clang 前端命令失败,退出代码为 70(使用 -v 查看调用)
- ios - 如何快速删除过去 14 天后的记录?
- angular - 如何以角度编写简单的ngrx代码以显示对象并将其添加到对象数组
- sql - 当我尝试将值插入表中时发生 Oracle 缺少表达式错误
- java - 为什么在静态上下文中访问实例化字段比在 JAVA 中访问实例化上下文更快
- angular - 当我尝试安装@angular/pwa 时。使用 ng 添加@angular/pwa。它向我显示了这个错误
- php - 我正在尝试用 Pusher 实现套接字
- javascript - 以特定方式解析嵌套对象数组
- go - 无法从超级账本结构中的链码实例将数据上传到谷歌云存储