解决python 3 vs python 2中的map函数问题 [英] resolving map function issue in python 3 vs python 2

查看:66
本文介绍了解决python 3 vs python 2中的map函数问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对使用python进行函数编程感兴趣,并正在通过Mary Rose Cook的博客帖子 ="a href =" https://maryrosecook.com/blog/post/a-practical-introduction-to-functional -programming"rel =" nofollow noreferrer>函数式编程的实用介绍 .

I'm interested in functional programming with python and am working through Mary Rose Cook's blog post A practical introduction to functional programming.

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

Apparently, it was written in python 2 as this:

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

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

在Python 3中

会产生以下结果:

in Python 3 yields this:

<map object at 0x100b87a20>

我有两个问题:

  1. 为什么会这样?
  2. 除了将地图对象转换为列表然后使用numpy 以外,还有其他解决方案吗?
  1. Why is this is so?
  2. Other than converting the map object to a list and then use numpy, are there any other solutions?

推荐答案

迁移指南中所述, /a>,

As documented, in the migration guide,

在Python 2中,map()返回一个列表,而在Python 3中,它返回一个迭代器.

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

Python 2 :

将函数应用于所有iterable,并返回结果的列表.

Python 3 :

返回一个迭代器,该迭代器将函数应用于所有可迭代项,并产生结果.

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

Python 2始终等同于list(imap(...)),Python 3允许进行惰性求值.

Python 2 always does the equivalent of list(imap(...)), Python 3 allows for lazy evaluation.

这篇关于解决python 3 vs python 2中的map函数问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆