【Python】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

 
 
posted @ 2017-11-03 23:13  OLIVER_QIN  阅读(88898)  评论(0编辑  收藏  举报