问题描述
有一列表
[sDe, abc, SDF]
问如何将该列表中的字符串全部做首字母大写处理并输出?
示例
输入:
[sDe, abc, SDF]
输出:
[Sde, Abc, Sdf]
解法一
使用
map
函数,高阶函数。
并使用
Lambda
函数作为高阶函数的参数。
lt = [sDe, abc, SDF]
mp = list(map(lambda x: x[0].upper() + x[1:].lower(), lt)) # map函数
print(mp)
map 函数的定义为:
map(func, *iterables) --> map object
Make an iterator that computes the function using arguments from
each of the iterables. Stops when the shortest iterable is exhausted.
第一个参数是一个函数,第二个参数是一个可变长参数。
翻译一下就是说创建一个迭代器,该迭代器使用每个可迭代对象的参数来计算函数。当最短的迭代次数用尽时停止。
在本例中就是说使用迭代访问
lt
,将每个迭代对象作为前面函数的调用参数返回。
解法二