gpt4 book ai didi

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

转载 作者:太空狗 更新时间:2023-10-29 18:14:28 25 4
gpt4 key购买 nike

我很好奇 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com