A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

map函数是Python里面比较重要的函数,设计灵感来自于函数式编程。Python官方文档中是这样解释map函数的:

map(function, iterable, …)

Return an iterator that applies function to every item of iterable, yielding the results. If additional iterable arguments are passed, function must take that many arguments and is applied to the items from all iterables in parallel. With multiple iterables, the iterator stops when the shortest iterable is exhausted.

即map函数接收的第一个参数为一个函数,可以为系统函数Axitrader返佣http://www.fx61.com/brokerlist/axitrader.html例如float、或者def定义的函数、或者lambda定义的函数均可。

举一个简单的例子,下面这个例子在Python2.7下是可以正常显示的:

ls = [1,2,3]

rs = map(str, ls)

#打印结果

['1', '2', '3']

lt = [1, 2, 3, 4, 5, 6]

def add(num):

return num + 1

rs = map(add, lt)

print rs

#[2,3,4,5,6,7]

但是在Python3下我们输入:

ls=[1,2,3]

rs=map(str,ls)

print(rs)

显示的却是:

而不是我们想要的结果,这也是Python3下发生的一些新的变化,如果我们想得到需要的结果需要这样写:

ls=[1,2,3]

rs=map(str,ls)

print(list(rs))

0 个回复

您需要登录后才可以回帖 登录 | 加入黑马