简体   繁体   English

python if语句不等于某些数字

[英]python if statement not equal to certain numbers

Is there a way to shorten this if statement? 有没有一种方法可以缩短此if语句?

if i != 9 or i != 23 or i != 25 or i != 33 or i !=35:
    print(i)

You can use a set and check if i is not in the set: 您可以使用一个集合并检查是否不在集合中:

 invalid_set = {9, 23,25, 33, 35} 
 if  i not in  invalid_set:
     # all good

A set lookup if O(1) vs O(n) with a list, tuple etc.. 如果O(1)O(n)具有列表,元组等,则进行集合查找。

how about 怎么样

if i not in [9,23,25,33,25]:
    print(i)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM