python中的map函数
2021-07-02 14:06
标签:bsp obj enum pre 循环 字母 index nbsp 字符串 python中的map函数 标签:bsp obj enum pre 循环 字母 index nbsp 字符串 原文地址:https://www.cnblogs.com/jiyanjiao-702521/p/9629731.html 1 def f(x):
2 return x * x
3
4
5 """将一个全是数字的list变成平方形式"""
6
7
8 def f2():
9 ls = [1, 2, 3, 4, 5]
10 r2 = map(f, ls)
11 print(list(r2))
12
13
14 """将list的值均变为字符串"""
15
16
17 def str2():
18 ls = [‘a‘, 2, 3, 4, 5]
19 ls2 = map(str, ls)
20 print(ls2)
21 print(list(ls2))
22
23
24 """将一个list里的首字母大写,后续字母小写"""
25
26
27 def format_name(s):
28 s1 = s[0:1].upper() + s[1:].lower()
29 return s1
30
31
32 def slower():
33 L = [‘jiyanjiao‘, ‘jiyanjiao‘, ‘jiyanjiao‘, ‘jiyanjiao‘]
34 m = 0
35 for i in L:
36 L[m] = i[0:1].upper() + i[1:]
37 m = m + 1
38 for index, name in enumerate(L):
39 print(index, ‘-‘, name)
40 # enumerate() 函数用于将一个可遍历的数据对象(如列表、元组或字符串)组合为一个索引序列,同时列出数据和数据下标,一般用在 for
41 # 循环当中。
42
43 f2()
44 str2()
45 slower()
46
47 """打印结果
48 [1, 4, 9, 16, 25]
49 50 [‘a‘, ‘2‘, ‘3‘, ‘4‘, ‘5‘]
51 0 - Jiyanjiao
52 1 - Jiyanjiao
53 2 - Jiyanjiao
54 3 - Jiyanjiao
55
56
57 """