python中的map函数

时间:2024-08-23 11:34:26
 def f(x):
return x * x """将一个全是数字的list变成平方形式""" def f2():
ls = [1, 2, 3, 4, 5]
r2 = map(f, ls)
print(list(r2)) """将list的值均变为字符串""" def str2():
ls = ['a', 2, 3, 4, 5]
ls2 = map(str, ls)
print(ls2)
print(list(ls2)) """将一个list里的首字母大写,后续字母小写""" def format_name(s):
s1 = s[0:1].upper() + s[1:].lower()
return s1 def slower():
L = ['jiyanjiao', 'jiyanjiao', 'jiyanjiao', 'jiyanjiao']
m = 0
for i in L:
L[m] = i[0:1].upper() + i[1:]
m = m + 1
for index, name in enumerate(L):
print(index, '-', name)
# enumerate() 函数用于将一个可遍历的数据对象(如列表、元组或字符串)组合为一个索引序列,同时列出数据和数据下标,一般用在 for
# 循环当中。 f2()
str2()
slower() """打印结果
[1, 4, 9, 16, 25]
<map object at 0x0000018601F31390>
['a', '2', '3', '4', '5']
0 - Jiyanjiao
1 - Jiyanjiao
2 - Jiyanjiao
3 - Jiyanjiao """