get all possible combinations of a list’s elements in python
April 16th, 2008 by Lawrence David
let’s say you’ve got a list of the numbers 0 through 4 and you wanted all 32 possible combinations of them. to get that list:
list_of_five = range(0,5)
swap_list_list = [[]]
for swap in list_of_five:
temp_lists = []
for list_stub in swap_list_list:
this_list = copy.copy(list_stub)
this_list.append(swap)
temp_lists.append(this_list)
temp_lists.append(list_stub)
swap_list_list = temp_lists
print swap_list_list

After doing “import copy” to your code, I get this output.
$ python combo.py
[[4], []]
[…] found some code (by googling) that apparently does what I’m looking for, but I found the code fairly opaque […]
Yes. You got this wrong.
When you don’t have import copy module this is completely useless.
[…] found some code (by googling) that apparently does what I’m looking for, but I found the code fairly opaque […]
[…] found some code (by googling) that apparently does what I’m looking for, but I found the code fairly opaque […]
oi
[…] 我有一个包含15个数字的列表,我需要编写一些代码,产生这些数字的所有32,768个组合。 我发现some code(由谷歌)显然是在做我正在寻找的,但我发现代码相当不透明,并且谨慎使用它。另外我有一种感觉,一定是一个更优雅的解决方案。 唯一发生在我身上的只是循环十进制整数1-32768并将其转换为二进制,并使用二进制表示作为过滤器来选出适当的数字。 有没有人知道更好的方法?使用map(),也许? […]
married pussy
[…] нашел некоторый код (по Googling), который, […]
[…] trovato un po ‘ di codice (da Google) che a quanto pare non è quello che sto cercando, ma ho trovato il codice abbastanza […]
悉尼的超级豪宅往往拥有无敌的海景、壮观的游艇码头、网球场、如此等等。
必威体育可靠么 http://www.amyter.com/
[…] encontrado código (buscando en Google) que al parecer hace lo que yo estoy buscando, pero me pareció que el código […]
[…] 코드를 찾았 습니다 찾고있는 것을 분명히하는 (구글링에 의해)를 찾았지만 코드가 상당히 불투명하고 사용하는 것에주의를 기울였습니다. 또한 더 우아한 솔루션이 있어야한다고 생각합니다. […]