从列表创建唯一组合 [英] Creating unique combinations from lists

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

问题描述

我有三个清单...例如


a = [''big'',''small'',''medium''];

b = [''old'',''new''];

c = [''blue'',''green''];


我想拿这些并最终得到他们所有的组合

创建如下列表

[''big'',''old'' ,''blue'']

[''小'',''旧'',''蓝'''

[''medium'',' 'old'',''blue'']

[''big'',''old'',''green'']

[''small '',''old'',''green'']

[''medium'',''small'',''green'']

[''big'',''new'',''blue'']

[''small'',''new'',''blue'']

[''medium'',''new'',''blue'']

[''big'',''new'',''gr een'']

[''small'',''new'',''green'']

[''medium'',''new' ',''green'']


我可以嵌套for ... in循环,但是正在寻找一种Pythonic方式

来做这件事。想法?

解决方案



-----原帖---- -

来自:py********************************@python.org [mailto :python-

li*************************@python.org]代表布雷尔

发送时间:2008年1月16日星期三下午2:15

要: py *********@python.org

主题:从列表中创建独特的组合


我有三个列表..例如


a = [''big'',''small'',''medium''];

b = [''old' ',''new''];

c = [''blue'',''green''];


我想拿这些和结束他们所有的组合

创建如下列表

[''big'',''old'',''blue'']

[''小'',''旧'',''蓝'''

[''我dium'',''old'',''blue'']

[''big'',''old'',''green'']

[''small'',''old'',''green'']

[''medium'',''small'',''green'']

[''big'',''new'',''blue'']

[''small'',''new'',''blue'' ]

[''中'',''新''''''蓝色'']

[''大'',''新'',' 'green'']

[''small'',''new'',''green'']

[''medium'',''new '',''green'']


我可以在循环中嵌套...但是正在寻找一种Pythonic方式

这样做。想法?


http:/ /www.python.org/dev/peps/pep-0202/


1月16日上午11:33,Reedick ,安德鲁 < jr9 ... @ ATT.COMwrote:


-----原始消息-----

来自:python-list-bounces + jr9445 = att .... @ python.org [mailto:python-

list-bounces + jr9445 = att .... @ python .org]代表布雷尔

发送时间:2008年1月16日星期三下午2:15

收件人:python-l ... @ python.org

主题:从列表中创建唯一组合


我有三个列表...例如

< blockquote class =post_quotes>
a = [''big'',''small'',''medium''];

b = [''old'',''新的''];

c = [''blue'',''green''];


我想拿这些并最终获得所有组合

创建如下列表

[''big'',''old'',''blue'']

[''小'',''旧'',''蓝''' ]

[''中'',''旧'',''蓝'''

[''大'',''老'',' 'green'']

[''small'',''old'',''green'']

[''medium'',''small '',''绿色'']

[''大'',''新'''''蓝色'']

[''小'' ,''new'',''blue'']

[''medium'',''new'',''blue'']

[' 'big'',''new'',''green'']

[''small'',''new'',''green'']

[''medium'',''new'',''green'']


我可以嵌套for ... in循环,但是正在寻找一个Pythonic方式

来做这件事。想法?


http:/ /www.python.org/dev/peps/pep-0202/



感谢您的回复。我从来没有意识到你可以像这样使用列表

理解...太棒了!


我可以嵌套for ... in循环,但是正在寻找一种Pythonic方式


来做这件事。想法?



我发现嵌套for循环非常Pythonic。明确比隐含更好,

和简单比复杂更好。


问候,

马丁


I have three lists... for instance

a = [''big'', ''small'', ''medium''];
b = [''old'', ''new''];
c = [''blue'', ''green''];

I want to take those and end up with all of the combinations they
create like the following lists
[''big'', ''old'', ''blue'']
[''small'', ''old'', ''blue'']
[''medium'', ''old'', ''blue'']
[''big'', ''old'', ''green'']
[''small'', ''old'', ''green'']
[''medium'', ''small'', ''green'']
[''big'', ''new'', ''blue'']
[''small'', ''new'', ''blue'']
[''medium'', ''new'', ''blue'']
[''big'', ''new'', ''green'']
[''small'', ''new'', ''green'']
[''medium'', ''new'', ''green'' ]

I could do nested for ... in loops, but was looking for a Pythonic way
to do this. Ideas?

解决方案


-----Original Message-----
From: py********************************@python.org [mailto:python-
li*************************@python.org] On Behalf Of breal
Sent: Wednesday, January 16, 2008 2:15 PM
To: py*********@python.org
Subject: Creating unique combinations from lists

I have three lists... for instance

a = [''big'', ''small'', ''medium''];
b = [''old'', ''new''];
c = [''blue'', ''green''];

I want to take those and end up with all of the combinations they
create like the following lists
[''big'', ''old'', ''blue'']
[''small'', ''old'', ''blue'']
[''medium'', ''old'', ''blue'']
[''big'', ''old'', ''green'']
[''small'', ''old'', ''green'']
[''medium'', ''small'', ''green'']
[''big'', ''new'', ''blue'']
[''small'', ''new'', ''blue'']
[''medium'', ''new'', ''blue'']
[''big'', ''new'', ''green'']
[''small'', ''new'', ''green'']
[''medium'', ''new'', ''green'' ]

I could do nested for ... in loops, but was looking for a Pythonic way
to do this. Ideas?


http://www.python.org/dev/peps/pep-0202/


On Jan 16, 11:33 am, "Reedick, Andrew" <jr9...@ATT.COMwrote:

-----Original Message-----
From: python-list-bounces+jr9445=att....@python.org [mailto:python-
list-bounces+jr9445=att....@python.org] On Behalf Of breal
Sent: Wednesday, January 16, 2008 2:15 PM
To: python-l...@python.org
Subject: Creating unique combinations from lists

I have three lists... for instance

a = [''big'', ''small'', ''medium''];
b = [''old'', ''new''];
c = [''blue'', ''green''];

I want to take those and end up with all of the combinations they
create like the following lists
[''big'', ''old'', ''blue'']
[''small'', ''old'', ''blue'']
[''medium'', ''old'', ''blue'']
[''big'', ''old'', ''green'']
[''small'', ''old'', ''green'']
[''medium'', ''small'', ''green'']
[''big'', ''new'', ''blue'']
[''small'', ''new'', ''blue'']
[''medium'', ''new'', ''blue'']
[''big'', ''new'', ''green'']
[''small'', ''new'', ''green'']
[''medium'', ''new'', ''green'' ]

I could do nested for ... in loops, but was looking for a Pythonic way
to do this. Ideas?


http://www.python.org/dev/peps/pep-0202/

Thanks for the reply. I never realized you could use list
comprehension like this... AWESOME!


I could do nested for ... in loops, but was looking for a Pythonic way

to do this. Ideas?

I find nested for loops very Pythonic. Explicit is better than implicit,
and simple is better than complex.

Regards,
Martin


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

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