在发电机上需要帮助...... [英] need help on generator...

查看:65
本文介绍了在发电机上需要帮助......的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好,


我正试图了解如何使用发电机从一个根连续设置后续连续设置




l = [1,2,3,4]

想生产:


[1, 2],[2,3],[3,4],[1,2,3],[2,3,4]

但不幸的是不能,我想我可以通过使用子生成器

并且可能枚举,如果你帮忙可以解释一下

技巧吗?看起来像这个子发生器让我搞砸了。


(我认为应该也有[1,],[2,],[3,],[1,2] ,3,4]和

然后被过滤掉)


bests

解决方案

2005年1月21日05:58:03 -0800
jo ****** @ yahoo.fr (Joh)写道:

我试图了解如何使用生成器从根目录中构建以下连续集合
< br => l = [1,2,3,4]
想生产:

[1,2],[2,3],[3, 4],[1,2,3],[2,3,4]


def consecutive_sets(l):
.... for x in xrange(2,len(l)):

.... for x in xrange(0,len(l) -i + 1):

....产量l [j:j + i]

.... list(consecutive_sets([1,2,3, 4]))



[[1,2],[2,3],[3,4],[1,2,3],[2,3,4]]


-

Denis S. Otkidach
http://www.python。 ru / [ru]


Joh写道:

你好,

我我试图理解我如何使用发电机从根部建立以下连续集合:

l = [1,2,3,4]
想制作:

[1,2],[2,3],[3,4],[1,2,3],[2,3,4]



你的意思是:


[1,2],[2,3],[3,4],[1, 2,3],[2,3,4],[1,3,4]

(例如除了空集之外的电源组中的所有元素,带有

的元素和所有元素的集合。)

否则,请用语言描述你想要的集合 - 我看不到

这一点。


周五,2005-01-21 17:14 + 0300,Denis S 。Otkidach写道:

2005年1月21日05:58:03 -0800
jo ** ****@yahoo.fr (Joh)写道:

我正在试图了解如何从根构建以下连续集合
一个使用发电机:

l = [1,2,3,4]
想生产:

[1,2], [2,3],[3,4],[1,2,3],[2,3,4]


def consecutive_sets(l):


... for x in xrange(2,len(l)):
... for x in xrange(0,len (l)-i + 1):
...产生l [j:j + i]


因为你的大脑比我快得多(尽管我最终得到了

完全相同的事情,除了变量名称)并打败我发布

答案,我会发布不可避免的生成器表达式版本

代替:


consecutive_sets =(x [offset :offset + subset_size]
$ x $ b for subset_size in xrange(2,len(x))

for offset in xrange(0,len(x)+ 1 - subset_size))


-

Craig Ringer


hello,

i''m trying to understand how i could build following consecutive sets
from a root one using generator :

l = [1,2,3,4]

would like to produce :

[1,2], [2,3], [3,4], [1,2,3], [2,3,4]

but unfortunately can not, i guess i can do it by using sub generator
and maybe enumerate, please if you help could you explain a bit the
trick ? looks like this sub generator thing mess me up.

(i think it should may be also have [1,], [2,], [3,], [1,2,3,4] , and
then be filtered)

bests

解决方案

On 21 Jan 2005 05:58:03 -0800
jo******@yahoo.fr (Joh) wrote:

i''m trying to understand how i could build following consecutive sets
from a root one using generator :

l = [1,2,3,4]

would like to produce :

[1,2], [2,3], [3,4], [1,2,3], [2,3,4]


def consecutive_sets(l): .... for i in xrange(2, len(l)):
.... for j in xrange(0, len(l)-i+1):
.... yield l[j:j+i]
.... list(consecutive_sets([1,2,3,4]))


[[1, 2], [2, 3], [3, 4], [1, 2, 3], [2, 3, 4]]

--
Denis S. Otkidach
http://www.python.ru/ [ru]


Joh wrote:

hello,

i''m trying to understand how i could build following consecutive sets
from a root one using generator :

l = [1,2,3,4]

would like to produce :

[1,2], [2,3], [3,4], [1,2,3], [2,3,4]


Do you mean:

[1,2], [2,3], [3,4], [1,2,3], [2,3,4], [1,3,4]
(E.g. all elements in the power set except the empty set, the sets with
one element and the sets with all elements.)
Otherwise, please describe your desired sets in verbal - I cannot see
the point.


On Fri, 2005-01-21 at 17:14 +0300, Denis S. Otkidach wrote:

On 21 Jan 2005 05:58:03 -0800
jo******@yahoo.fr (Joh) wrote:

i''m trying to understand how i could build following consecutive sets
from a root one using generator :

l = [1,2,3,4]

would like to produce :

[1,2], [2,3], [3,4], [1,2,3], [2,3,4]


def consecutive_sets(l):


... for i in xrange(2, len(l)):
... for j in xrange(0, len(l)-i+1):
... yield l[j:j+i]



Since you have a much faster brain than I (though I ended up with
exactly the same thing barring variable names) and beat me to posting
the answer, I''ll post the inevitable awful generator expression version
instead:

consecutive_sets = ( x[offset:offset+subset_size]
for subset_size in xrange(2, len(x))
for offset in xrange(0, len(x) + 1 - subset_size) )

--
Craig Ringer


这篇关于在发电机上需要帮助......的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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