而不是编写许多和如何以pythonic方式缩短代码

时间:2018-05-30 17:56:37

标签: python python-3.x python-2.7

sv= 0.000324
if sv != 0.00002 and 0.00003 and 0.00004 and 0.00005 and 0.0002 and 0.0003 and 0.0004 and 0.0005 and 0.000003 and 0.04 and 0.000000090 and 0.000005 and 0.00003 and 0.000000090 and 0.0000003:
    print (sv)

而不是编写许多ands或ors是否有办法使这段代码更短或更pythonic方式?

1 个答案:

答案 0 :(得分:2)

将所有值放入列表中,然后选中不用。示例如下

sv= 0.000324
not_sv_list = [0.00002, 0.00003, 0.00004, 0.00005, 0.0002, 0.0003, 0.0004, 
        0.0005, 0.000003, 0.04, 0.000000090, 0.000005, 0.00003, 0.000000090, 0.0000003 ]
if sv not in not_sv_list:
    print (sv)