首页 > 解决方案 > 检查列表是否包含除“apples”和“oranges”之外的其他字符串

问题描述

如何知道 python 中的列表是否包含除'apples'and之外的其他元素'oranges'

fruits = ['apples', 'oranges']

现在,如果列表 fruits 包含任何其他字符串假设:

fruits = ['apples', 'oranges', grapes'] 或者 fruits = ['oranges', grapes']

我该如何检查?

标签: pythonlist

解决方案


试试下面的代码:

fruits = ['apples', 'oranges', 'grapes']

for i in fruits:
    if i not in ['apples','oranges']:
       print i  # Mismatched values will be printed

输出:

grapes

推荐阅读