解决Python3下map函数的显示问题

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

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.app

即map函数接收的第一个参数为一个函数,能够为系统函数例如float、或者def定义的函数、或者lambda定义的函数都可。函数式编程

举一个简单的例子Axitrader返佣http://www.kaifx.cn/broker/ax...,下面这个例子在Python2.7下是能够正常显示的:函数

ls = [1,2,3]设计

rs = map(str, ls)htm

打印结果

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

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

def add(num):get

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))

相关文章
相关标签/搜索