python - 解决 python 3 与 python 2 中的 map 函数问题

标签 python python-3.x python-2.7 functional-programming

<分区>

我对使用 Python 进行函数式编程很感兴趣,并且正在研究 Mary Rose Cook 的博客文章 A practical introduction to functional programming .

显然,它是用 python 2 编写的:

name_lengths = map(len, ["Mary", "Isla", "Sam"])

print name_lengths
# => [4, 4, 3]

在 Python 3 中产生这个:

<map object at 0x100b87a20>

我有两个问题:

  1. 为什么会这样?
  2. converting the map object to a list and then use numpy 除外,还有其他解决办法吗?

最佳答案

如文件所示,在 migration guide 中,

In Python 2 map() returns a list while in Python 3 it returns an iterator.

Python 2 :

Apply function to every item of iterable and return a list of the results.

Python 3 :

Return an iterator that applies function to every item of iterable, yielding the results.

Python 2 始终执行与 list(imap(...)) 等价的操作,Python 3 允许惰性求值。

关于python - 解决 python 3 与 python 2 中的 map 函数问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45678914/

相关文章:

python - 在循环中绘制图形

python - 将 forMine 设置为 false 进行搜索时出现 youtube v3 api 错误

页面上的 Javascript 阻止 Selenium 检测某些框架和元素

python-3.x - 如何在列表的解析参数中解析列表(字符串)而不是列表(字符)?

python - Python 或 MySQL 中是否有一个内置函数可以返回一组 3 个经常出现的单词?

python - Gtk Keybinder 没有响应

python - 为什么使用参数的 pandas.read_sql 比使用内联参数慢得多

python - 使用 Python 3 打印 HTML 数据

python - 将转义字符写入文件

python 一次循环遍历行数