Python:怎样打印map中内容

最近更新时间 2020-02-10 18:38:26

Python2中,直接用 print 函数可以打印 map 中的内容,但在 Python3 中,map 返回的结果是迭代器,需要使用 list 函数显示 map 中的结果。

1. 直接打印结果

oneList = [1, 2, 3, 4, 5]
twoList = map(lambda n:n*n, oneList)
print(twoList)
<map object at 0x7fc61adca310>

2. 打印map中内容

oneList = [1, 2, 3, 4, 5]
twoList = map(lambda n:n*n, oneList)
print(list(twoList))
[1, 4, 9, 16, 25]
rss_feed