不乱于心,不困于情。
不畏将来,不念过往。如此,安好。

Python中in与not in

在python中,要判断特定的值是否存在列表中,可使用关键字in,判断特定的值不存在列表中,可使用关键字not in

letters = ['A','B','C','D','E','F','G']
if 'A' in letters:
    print('A'+' exists')
if 'h' not in letters:
    print('h'+' not exists')

打印结果:

A exists
h not exists

一个稍微复杂的案例

定义2个列表分别为list_01跟list_02

list_01 = ['A','B','C','H']
list_02 = ['A','B','E','F']

通过判断查看list_01在list_02中的存在情况
list_01 = ['A','B','C','H']
list_02 = ['A','B','E','F']

for letter in list_01:
    if letter in list_02:
        print(letter+' is exists')
    else:
        print(letter+' is not exists')

打印结果:

A is exists
B is exists
C is not exists
H is not exists

赞(0)
未经允许不得转载:seo优化_前端开发_渗透技术 » Python中in与not in