标签:nbsp turn log pre res highlight function ble lis
# map()函数使用举例 # 功能:map()接受一个函数f和一个或多个list,将f依次作用在list的每个元素,得到一个新的列表 # 语法:map(方法名,列表,[列表2]) # 注意:map()函数的返回值需要强制转换成list类型,且不改变原列表值 list_1 = [1, 2, 3, 4, 5] list_2 = [1, 2, 3, 4, 5] # 单个参数 def double_function(number): return number * 2 list_result = list(map(double_function, list_1)) print("单参数map结果:", list_result) # 多个参数 def multiply_function(m, n): return m * n list_result = list(map(multiply_function, list_1, list_2)) print("多参数map结果:", list_result)
运行结果:
单参数map结果: [2, 4, 6, 8, 10] 多参数map结果: [1, 4, 9, 16, 25]
标签:nbsp turn log pre res highlight function ble lis
原文地址:http://www.cnblogs.com/gongxr/p/7247855.html