map(function , sequemce):对sequence中的item依次执行function(item),执行结果组成一个List返回,其中function即可以是一个内置函数,也可以是自定义的函数或者是lambda表达式
第一种:function是Python内置函数
def demo():
str_list = ["china", "is", "beauty"]
name_len = list(map(len, str_list))
print(type(name_len), name_len)
输出结果:
<class 'list'> [5, 2, 6]
第二种:function是一个自定义的函数
def demo():
def toUpper(item):
return item.upper()
str_list = ["china", "is", "beauty"]
new_list = list(map(toUpper, str_list))
print(new_list)
输出结果:
['CHIN