Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/282.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在python中按名称忽略忽略列表中的项_Python_List_Python 2.7 - Fatal编程技术网

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

在python中按名称忽略忽略列表中的项,python,list,python-2.7,Python,List,Python 2.7,我想在python中按名称忽略ignore_列表中的所有项。例如,考虑 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 我希望好的列表也忽略“桃子派

我想在python中按名称忽略ignore_列表中的所有项。例如,考虑

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

我希望好的列表也忽略“桃子派”,因为桃子在过敏列表中,桃子派包含桃子:-p

您需要做的就是实现类似的东西。这取决于您计划使用的字符串的格式,但它适用于本例。只需将其添加到示例代码的末尾。请随时询问未来的澄清或如何处理水果列表中条目的其他格式

good_list2=[]
for entry in good_list:
    newEntry=entry.split(' ')
    for split in newEntry:
        if not split in allergy_list:
             good_list2.append(split)

 print good_list2

你需要做的就是实现这样的东西。这取决于您计划使用的字符串的格式,但它适用于本例。只需将其添加到示例代码的末尾。请随时询问未来的澄清或如何处理水果列表中条目的其他格式

good_list2=[]
for entry in good_list:
    newEntry=entry.split(' ')
    for split in newEntry:
        if not split in allergy_list:
             good_list2.append(split)

 print good_list2
那么:

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']
那么:

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']

“无懈可击的意大利香肠”里有桃子吗?@DSM你在什么样的餐馆吃饭?:)@DeepakB:不需要
:-(
,你应该是
:-)
!如果你不必担心把事情分解成文字,只需要检测子字符串,那就容易多了。太棒了:-)谢谢大家@Deepak如果您不必拆分列表,我建议只使用
f.find(allergy_item,0,len(f))
查看
allergy_item
字符串是否出现在
水果列表中。您似乎喜欢列表理解,但这可以通过使用它来实现;)“无懈可击的意大利香肠”里有桃子吗?@DSM你在什么样的餐馆吃饭?:)@DeepakB:不需要
:-(
,你应该是
:-)
!如果你不必担心把事情分解成文字,只需要检测子字符串,那就容易多了。太棒了:-)谢谢大家@Deepak如果您不必拆分列表,我建议只使用
f.find(allergy_item,0,len(f))
查看
allergy_item
字符串是否出现在
水果列表中。您似乎喜欢列表理解,但这可以通过使用它来实现;)<代码>[f代表水果中的f,如果没有(a代表过敏性水果中的f.split())
[f代表水果中的f,如果没有(a代表过敏性水果中的f.split())