从列表中提取所有n个大小的组合 [英] Pulling all n-sized combinations from a list

查看:71
本文介绍了从列表中提取所有n个大小的组合的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好,


我有一个合理大小的物品清单我想拉出来

五种组合来自的元素。现在我有办法做这个'b $ b'这个很慢,但是可以管理。我知道必须有更好的方法才能做到这一点,但我不确定它是什么。这就是我所拥有的所以

远:


for myList:

for my in myList :

如果a == b:

中断

for my in myList:

如果b == c :

休息
我的列表中的


如果c == d:

break

for my in myList:

如果d == e:

break

#我的代码在这里。


残酷而缓慢,我很有信心,但还有更好的方法吗?我不能用b $ b来创建一个包含我想要的组合的列表,因为它不适合内存中的
。并且我确信我可以使用标准范例使用

五为索引编制循环(即,对于i = 1,对于j = i + 1,对于k = j + 1,等等。)。

但是有没有一种非常好的方法可以用Python来处理它?<​​br />

感谢您的时间!

Scott

解决方案

" Swroteb" < SW ******* @ gmail.com>写道:

残酷而缓慢,我很确定,但还有更好的方法吗?我不能简单地创建一个包含我想要的组合的列表,因为它不适合内存。我确信我可以使用标准范例使用
五个索引的循环(即,对于i = 1,对于j = i + 1,对于k = j + 1等)。<但是,有没有一种非常好的方法可以在Python中处理它?<​​/ blockquote>


这是一个功课问题吗?提示:1)使用递归; 2)使用发电机。




Paul Rubin写道:

" Swroteb" < SW ******* @ gmail.com>写道:

残酷而缓慢,我很确定,但还有更好的方法吗?我不能简单地创建一个包含我想要的组合的列表,因为它不适合内存。我确信我可以使用标准范例使用
五个索引的循环(即,对于i = 1,对于j = i + 1,对于k = j + 1等)。<但是,有没有一种非常好的方法可以在Python中处理它?<​​/ blockquote>

这是一个功课问题吗?提示:1)使用递归; 2)使用发电机。




我很感激回应;不,这不是一个家庭作业问题。


我对你的发电机建议有点困惑。我的列表是

一组对实例化对象的引用。我只是不确定

如何迭代这些引用的每个独特组合。是否

你建议我设置方法来提供我想要的指数

在列表中迭代过来?


" Swroteb" < SW ******* @ gmail.com>写道:

我对你的发电机建议有点困惑。我的列表是一组对实例化对象的引用。我只是不确定如何遍历这些引用的每个独特组合。您是否建议我设置方法以提供我正在寻找的索引以便在列表中迭代?




I认为自然的方法是制作一个生成器,为每个组合产生一个
5元组,然后让你的应用程序在该生成器上迭代

。这是我的版本:


def comb(x,n):

"""生成列表x中n个项目的组合 "

如果n == 0:

收益率[]

返回
$ x $ b for i in xrange (len(x)-n + 1):

为梳子中的r(x [i + 1:],n-1):

收益率[x [i ]] + r


for c in comb([1,2,3,4,5],3):

print c


输出为:


[1,2,3]

[1,2,4]

[1,2,5]

[1,3,4]

[1,3,5]

[1,4,5]

[2,3,4]

[2,3] 5]

[2,4,5]

[3,4,5]



Hi there,

I''ve got a reasonably sized list of objects that I''d like to pull out
all combinations of five elements from. Right now I have a way to do
this that''s quite slow, but manageable. I know there must be a better
way to do this, but I''m not sure what it is. Here''s what I''ve got so
far:

for a in myList:
for b in myList:
if a == b:
break
for c in myList:
if b == c:
break
for d in myList:
if c == d:
break
for e in myList:
if d == e:
break
# my code here.

Atrocious and slow, I''m sure, but is there a better way? I can''t
simply create a list with the combinations I want, since it won''t fit
into memory. And I''m sure I can do it using a standard paradigm using
five indexed for loops (ie, for i = 1, for j = i+1, for k = j+1, etc.).
But is there a really nice way to handle this in Python?

Thanks for your time!
Scott

解决方案

"Swroteb" <sw*******@gmail.com> writes:

Atrocious and slow, I''m sure, but is there a better way? I can''t
simply create a list with the combinations I want, since it won''t fit
into memory. And I''m sure I can do it using a standard paradigm using
five indexed for loops (ie, for i = 1, for j = i+1, for k = j+1, etc.).
But is there a really nice way to handle this in Python?



Is this a homework problem? Hint: 1) use recursion; 2) use generators.



Paul Rubin wrote:

"Swroteb" <sw*******@gmail.com> writes:

Atrocious and slow, I''m sure, but is there a better way? I can''t
simply create a list with the combinations I want, since it won''t fit
into memory. And I''m sure I can do it using a standard paradigm using
five indexed for loops (ie, for i = 1, for j = i+1, for k = j+1, etc.).
But is there a really nice way to handle this in Python?



Is this a homework problem? Hint: 1) use recursion; 2) use generators.



I appreciate the response; no, this is not a homework problem.

I''m a little bit confused about your generator suggestion. My list is
a set of references to instantiated objects. I''m just unsure about how
to iterate through every unique combination of those references. Are
you suggesting that I set up methods to provide the indices I''m looking
for in the list to iterate over?


"Swroteb" <sw*******@gmail.com> writes:

I''m a little bit confused about your generator suggestion. My list is
a set of references to instantiated objects. I''m just unsure about how
to iterate through every unique combination of those references. Are
you suggesting that I set up methods to provide the indices I''m looking
for in the list to iterate over?



I think the natural approach is to make a generator that yields a
5-tuple for each combination, and then have your application iterate
over that generator. Here''s my version:

def comb(x,n):
"""Generate combinations of n items from list x"""
if n==0:
yield []
return
for i in xrange(len(x)-n+1):
for r in comb(x[i+1:], n-1):
yield [x[i]] + r

for c in comb([1,2,3,4,5], 3):
print c

The output is:

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



这篇关于从列表中提取所有n个大小的组合的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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