1 l = [34,3,5]
2 l.sort()
3 print(l)
4 运行结果:[3, 5, 34]
5
6 ---------------------------------------------------------------------------------------------------------
7 s = '32456565436'
8 l = [34,3,5]
9
10 print(sorted(l))
11 print(sorted(s))#字符串排序之后以列表的形式返回了
12 print(sorted(l,reverse=True))#反转
13 运行结果:
14 [3, 5, 34]
15 ['2', '3', '3', '4', '4', '5', '5', '5', '6', '6', '6']
16 [34, 5, 3]
1 map & filter
2 map #循环调用函数,获取到函数的返回结果
3 filter #过滤,循环调用函数,把传入的参数,真的保留下来
4
5 #不用zfill方法 用函数实现补0
6
7 def zfill(num):
8 num = str(num)
9 if len(num)==1:
10 num = '0'+num
11 return num
12
13 #实现01-33
14 【第一种实现方法】
15 l = []
16 for i in range(1,34):
17 result = zfill(i)
18 l.append(result)
19 print(l)
20 运行结果:['01', '02', '03', '04', '05', '06', '07', '08', '09', '10', '11', '12', '13', '14', '15', '16', '17', '18', '19', '20', '21', '22', '23', '24', '25', '26', '27', '28', '29', '30', '31', '32', '33']
21
22 #列表生成式的方法 【第二种实现方法】
23 l = [zfill(i) for i in range(1,34)]
24 print(l)
25
26 #map方法 【第三种实现方法】
27 # result = map(zfill,range(1,34)) #生成的是map对象,所以要转换成list才能看到结果
28 #map第一个参数要指定函数名 #只能传一个参数
29 result = list(map(zfill,range(1,34))) #map循环去调用函数
30 print(result)
31
32 #创建多个文件夹
33 import os
34 list(map(os.mkdir,['hah','hei','bai']))
35
36 -------------------------------------------------------------------------------------------------------------------
1 score = [89,46,60,52,45,37,66,100,21,24]
2
3 def is_jg(s):
4 if s>59:
5 return True
6
7 jg = []
8 for i in score:
9 if is_jg(i):
10 jg.append(i)
11 print(jg)
12 运行结果:[89, 60, 66, 100]
13 -------------------------------------------------------------------------------------------------------------
14 score = [89,46,60,52,45,37,66,100,21,24]
15
16 def is_jg(s):
17 if s>59:
18 return True
19
20 result = list(filter(is_jg,score))
21 result2 = list(map(is_jg,score))
22
23 print('filter',result)
24 print('map',result2)
25 运行结果:filter [89, 60, 66, 100]
26 map [True, None, True, None, None, None, True, True, None, None]
![]()