使用or和not运算符

时间:2013-10-12 19:15:07

标签: python operators

想知道是否有人可以帮助我。我需要检查成员是否等于3个不同的字符串。我尝试过使用or,但没有成功。代码是:

list = ["a", "b", "c", "d"]

for member in list:
    if not member == "a" or not member == "b" or not  member == "c":
        print member 

我只需打印"d"。我只需要"d"

2 个答案:

答案 0 :(得分:3)

您只需要一次in测试:

for member in list:
    if member not in ('a', 'b', 'c'):
        print member 

答案 1 :(得分:0)

如果您希望代码正常工作,您只需要将'或'替换为'和'。尽管如此,Rohit Jain的答案仍然是表达同样事情的更好方式。