map函数


1、map函数会根据自定义函数的条件来操作和遍历序列中每个元素

2、map函数的返回值是可迭代对象

L = [1, 2, 3, 4, 5, 6, 7, 8, 9]
nums = map(lambda x: x ** 2, L)
newlist = list(nums)
print(newlist)

# 可迭代对象可以使用for语句进行遍历
for x in newlist:
    print(x)

# 简写
print(list(map(lambda x: x ** 2, [1, 2, 3, 4, 5, 6, 7, 8, 9])))

results matching ""

    No results matching ""