python - itertools.imap vs 映射整个可迭代对象

标签 python python-2.x python-itertools

我很好奇 http://docs.python.org/2/library/itertools.html#itertools.imap 的声明, 即描述

sum(imap(operator.mul, vector1, vector2))

作为高效的点积。我的理解是 imap 提供了一个生成器而不是一个列表,虽然我理解如果您只考虑前几个元素以及周围的 sum() 会更快/消耗更少的内存,但我不知道如何它的行为与以下任何不同:

sum(map(operator.mul, vector1, vector2))

最佳答案

mapimap 之间的区别在您开始增加要迭代的内容的大小时变得很明显:

# xrange object, takes up no memory
data = xrange(1000000000)

# Tries to builds a list of 1 billion elements!
# Therefore, fails with MemoryError on 32-bit systems.
doubled = map(lambda x: x * 2, data)

# Generator object that lazily doubles each item as it's iterated over.
# Takes up very little (and constant, independent of data's size) memory.
iter_doubled = itertools.imap(lambda x: x * 2, data)

# This is where the iteration and the doubling happen.
# Again, since no list is created, this doesn't run you out of memory.
sum(iter_doubled)

# (The result is 999999999000000000L, if you're interested.
# It takes a minute or two to compute, but consumes minimal memory.)

请注意,在 Python 3 中,内置 map 的行为类似于 Python 2 的 itertools.imap(因为不再需要而被删除)。要获得“旧的 map”行为,您可以使用 list(map(...)),这是另一种可视化 Python 2 的 itertools.imapmap 彼此不同。

关于python - itertools.imap vs 映射整个可迭代对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20860181/

相关文章:

python - 依赖于先前匹配的正则表达式

python - 为什么用 python3 用正确的逻辑计算数字中的数字会出现错误的答案?

Python2 : Should I use Pickle or cPickle?

python - 特征 : **kwargs allowing improperly named variables

python - 如何确定创建闭包的函数?

python - 匹配列表中的一个元素,然后返回它之前的 `n`个元素和它之后的 `m`个元素

python - 将 (X, Y) 组合列表相乘

python - 如何对二叉搜索树中给定值下的所有节点求和?

python - 从嵌套列表中删除相似元素

python - 在 Flask 响应头中设置 Unicode 文件名