所有可能的组合 [英] all possible combinations

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

问题描述

假设我有一个包含3个字母的列表:


[''a'',''b'',''c'']


我想打印这些3

字母的所有可能的4位数组合:


4 ^ 3 = 64


aaaa

abaa

aaba

aaab

acaa
aaca

aaac

....


最有效的方法是什么?

Say I have a list that has 3 letters in it:

[''a'', ''b'', ''c'']

I want to print all the possible 4 digit combinations of those 3
letters:

4^3 = 64

aaaa
abaa
aaba
aaab
acaa
aaca
aaac
....

What is the most efficient way to do this?

推荐答案

2005年7月13日星期三10:21:19 -0400,rbt写道:
On Wed, 13 Jul 2005 10:21:19 -0400, rbt wrote:
Say我有一个包含3个字母的列表:

['''',''b'',''c'']

我想要打印这3个字母的所有可能的4位数组合:

4 ^ 3 = 64
aaaa
abaa
aaba aaab
acaa
aaca
aaac


最有效的方法是什么?
Say I have a list that has 3 letters in it:

[''a'', ''b'', ''c'']

I want to print all the possible 4 digit combinations of those 3
letters:

4^3 = 64

aaaa
abaa
aaba
aaab
acaa
aaca
aaac
...

What is the most efficient way to do this?




对谁有效?用户?程序员?电脑?高效使用

的速度或内存或开发时间?


如果你想要最快的运行时效率,查询表是

预先计算的值。这是一个O(1)操作,你没有比那更快获得



如果你希望将程序扩展到任意列表,预先计算

是不切实际的,所以你需要一个算法来计算排列(订单

很重要)或组合(顺序没关系)。


如果你告诉我们你需要什么,我相信有人可以帮助满足这些需求。否则,我们只是猜猜你想要什么。


-

史蒂文。



Efficient for who? The user? The programmer? The computer? Efficient use
of speed or memory or development time?

If you want the fastest runtime efficiency, a lookup table of
pre-calculated values. That is an O(1) operation, and you don''t get any
faster than that.

If you expect to extend the program to arbitrary lists, pre-calculation
isn''t practical, so you need an algorithm to calculate permutations (order
matters) or combinations (order doesn''t matter).

If you tell us what you need, I''m sure somebody will be able to help meet
those needs. Otherwise, we''re just guessing what you want.

--
Steven.


On Thu,2005-07-14 at 00:47 +1000,Steven D''Aprano写道:
On Thu, 2005-07-14 at 00:47 +1000, Steven D''Aprano wrote:
2005年7月13日星期三10:21:19 -0400,rbt写道:
On Wed, 13 Jul 2005 10:21:19 -0400, rbt wrote:
说我有一个包含3个字母的列表:

['''',''b'',''c '']

我想打印这些3
字母的所有可能的4位数组合:

4 ^ 3 = 64
aaa
abaa
aaba
aaab
acaa
aaca
aaac


什么是最有效的方法吗?
对谁有效?用户?程序员?电脑?高效使用速度或内存或开发时间?
Say I have a list that has 3 letters in it:

[''a'', ''b'', ''c'']

I want to print all the possible 4 digit combinations of those 3
letters:

4^3 = 64

aaaa
abaa
aaba
aaab
acaa
aaca
aaac
...

What is the most efficient way to do this?
Efficient for who? The user? The programmer? The computer? Efficient use
of speed or memory or development time?




CPU

如果你想要最快的运行时效率,查找
预先计算的值表。这是一个O(1)操作,你不会比这更快。

如果你希望将程序扩展到任意列表,预先计算不实用,所以你需要一个算法来计算排列(订单重要)或组合(顺序没关系)。



The CPU

If you want the fastest runtime efficiency, a lookup table of
pre-calculated values. That is an O(1) operation, and you don''t get any
faster than that.

If you expect to extend the program to arbitrary lists, pre-calculation
isn''t practical, so you need an algorithm to calculate permutations (order
matters) or combinations (order doesn''t matter).




我的清单不是随意的。我正在寻找所有''组合'',因为我最初发布了
。订单对我来说无关紧要......只是所有

的可能性。



My list is not arbitrary. I''m looking for all ''combinations'' as I
originally posted. Order does not matter to me... just all
possibilities.


2005年3月13日星期三:21 -0400,rbt写道:
On Wed, 2005-07-13 at 10:21 -0400, rbt wrote:
说我有一个包含3个字母的列表:

['''',''b'',' 'c'']

我想打印这些3
字母的所有可能的4位数组合:

4 ^ 3 = 64
< bra> aaaa
abaa
aaba
aaab
acaa
aaca
aaac


什么这是最有效的方法吗?
Say I have a list that has 3 letters in it:

[''a'', ''b'', ''c'']

I want to print all the possible 4 digit combinations of those 3
letters:

4^3 = 64

aaaa
abaa
aaba
aaab
acaa
aaca
aaac
...

What is the most efficient way to do this?




将此扩展为4 ^ 4(256)以测试random.sample函数生成

有趣的结果。它从来没有找到超过24个组合的

可能256个。这导致了一个问题......'随机'是怎样的样本;)


试试吧:


test = list(''1234'')


组合= []

而1:

combo = random.sample(test,4)

possible =''''。join(combo)

如果可能不是组合:

打印可能性

combination.append(可能性)

继续

else:

继续



Expanding this to 4^4 (256) to test the random.sample function produces
interesting results. It never finds more than 24 combinations out of the
possible 256. This leads to the question... how ''random'' is sample ;)

Try it for yourselves:

test = list(''1234'')

combinations = []
while 1:
combo = random.sample(test, 4)
possibility = ''''.join(combo)
if possibility not in combinations:
print possibility
combinations.append(possibility)
continue
else:
continue


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

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