将Python生成器解压缩为参数-内存有效吗? [英] Unpacking a Python generator into arguments - memory efficient?

查看:47
本文介绍了将Python生成器解压缩为参数-内存有效吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

说我有一个sets的生成器:

def f(n) :
  for i in xrange(n) :
    yield set(xrange(i) )

>>> for s in f(5) :
      print s

set([])
set([0])
set([0, 1])
set([0, 1, 2])
set([0, 1, 2, 3])

现在我要union它们.我可以创建一组临时列表,然后将该列表解压缩为union的参数:

Now I want to union them. I can create a temporary list of sets, and unpack that list into arguments for union:

>>> set.union( * list( f(5) ) )
set([0, 1, 2, 3])

我还可以将生成器本身分配给union:

I can also give the generator itself to union:

>>> set.union( * f(5) )
set([0, 1, 2, 3])

第二种方法是否像第一种方法一样创建完整的临时列表?哪种方法对内存有效?

Does the second approach create the full temporary list like the first one? Which approach is memory efficient?

推荐答案

在将生成器用作参数时,Python首先扩展生成器 first .这两个选项中,在调用发生之前,生成器生成的所有值都将被加载到内存中.

Python expands a generator first when applying it as arguments; all values produced by the generator are loaded into memory before the call takes place, in both options.

您可以改用 reduce()函数调用:

You could use a reduce() function call instead:

from functools import reduce  # Python 3 forward compatibility

reduce(set.union, f(5))

这一步一步地遍历了f(5)产生的值,而无需先建立它们的序列.

This iterates over the values produced by f(5) one by one without building up a sequence of them first.

演示:

>>> def f(n):
...     for i in xrange(n):
...         yield set(xrange(i))
... 
>>> reduce(set.union, f(5))
set([0, 1, 2, 3])

这篇关于将Python生成器解压缩为参数-内存有效吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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