python - 在python中按名称忽略忽略列表中的项目

标签 python list python-2.7

我想在 python 中按名称忽略 ignore_list 中的所有项目。例如考虑

fruit_list = ["apple", "mango", "strawberry", "cherry", "peach","peach pie"]
allergy_list = ["cherry", "peach"]
good_list = [f for f in fruit_list if (f.lower() not in allergy_list)]
print good_list

我希望 good_list 也忽略“桃子派”,因为桃子在过敏列表中并且桃子派包含桃子:-P

最佳答案

怎么样:

fruits = ["apple", "mango", "strawberry", "cherry", "peach","peach pie"]
allergies = ["cherry", "peach"]

okay = [fruit for fruit in fruits if not any(allergy in fruit.split() for allergy in allergies)]
# ['apple', 'mango', 'strawberry']

关于python - 在python中按名称忽略忽略列表中的项目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17709045/

相关文章:

python - 分别获取查询集中每个项目的计数 Django DRF

python - Scipy - Nan 计算马氏距离时

python - 当列包含 `==` 而不是 `List` 时, Pandas 比较运算符 `Tuple` 无法按预期工作

python - 如何使用 python 自动更改 html <input> 标签?

python-2.7 - 在python中使用sftp从远程路径获取文件到本地目录

python - 按需将 S3 存储到 Glacier 是否可以从 boto3 API 获得?

python - 使用Python将Json数据转换为SQL表

python - 知道是否有列表列表 - Python

c# - 从异步函数返回的 IEnumerable<T> 创建 List<T>

python - 如何将对象作为参数传递给函数?