如果条件为python3的多值检查

时间:2018-07-30 07:57:53

标签: python python-3.x if-statement

如何在如下所示的if条件下执行多值运算,我尝试了但没有用

if 'mydj-lb-foxdc01' or 'tsdj-lb-foxdc02' or 'mydj-lb-noivm01' or 'mydj-lb-noivm02' not in line:

下面正在尝试时工作。.

if 'mydj-lb-foxdc01' not in line:

3 个答案:

答案 0 :(得分:0)

您可以使用any()内置方法:

line = 'mydj-lb-noivm01'

if not any(['mydj-lb-foxdc01' in line, 'tsdj-lb-foxdc02' in line, 'mydj-lb-noivm01' in line, 'mydj-lb-noivm02' in line]):
    print('not in line')
else:
    print('in line')

输出:

in line

答案 1 :(得分:0)

line = 'mydj-lb-noivm01'

if not any(['mydj-lb-foxdc01' in line, 'tsdj-lb-foxdc02' in line, 'mydj-lb-noivm01' in line, 'mydj-lb-noivm02' in line]):
    print('not in line')
else:
    print('in line')

答案 2 :(得分:-1)

line = 'mydj-lb-noivm01'

if not any(['mydj-lb-foxdc01' in line, 'tsdj-lb-foxdc02' in line, 'mydj-lb- 
noivm01' in line, 'mydj-lb-noivm02' in line]): 
print('not in line') 
else: 
print('in line')