filter

 

filter
 1 def larger100(x):
 2     if x>100:
 3         return True
 4     else:
 5         return False
 6 for item in filter(larger100,[2,200,100]):
 7     print(item)               #打印200
 8 a_list = [5,9,6,7,2,8,1,4,3,0]
 9 b_list = [5,9,6,7,9,3,4,5,9,2]
10 b_miss = [None if b_list.__contains__(j) else j for j in a_list]
11 b_miss1 = [j for j in a_list if not(b_list.__contains__(j))]
12 print(type(b_miss))           #打印<class 'list'>
13 print('b_miss:',b_miss)       #b_miss: [None, None, None, None, None, 8, 1, None, None, 0]
14 print('b_miss1:',b_miss1)     #b_miss1: [8, 1, 0]
15 b_filter = filter(None,b_miss)#打印 [8,1] 0被过滤
16 b_filter1= filter(lambda x:x != None,b_miss)
17 print(type(b_filter))         #打印<class 'filter'>
18 print(list(b_filter))         #[8, 1]
19 print(list(b_filter1))        #[8, 1, 0]
View Code

 b_filter1= filter(lambda x:x is not None,b_miss)也可

上一篇:并行计算的基本概念与方法(1)-缓存与存储层次


下一篇:cf1281 F. Miss Punyverse