[英]Generate all possible 2 and 3 string combinations from a list in python
[英]Generate all combinations from a List in python
有沒有更簡單的方法來做到這一點,這不那么凌亂
array = []
for i in [True, False]:
for j in [True, False]:
for k in [True, False]:
for l in [True, False]:
for m in [True, False]:
for n in [True, False]:
for o in [True, False]:
tup = (i,j,k,l,m,n,o)
array.append(tup)
print (len(array))
for tup in array:
print (tup)
這給了我一個大小為 128 的列表,其中包含 (true,false,false..) 組合的所有組合。
這是使用itertools.combinations
的解決方案
from itertools import combinations
result = set(combinations((True, False) * 7, 7))
for tup in result:
print(tup)
print(f"TOTAL: {len(result)}")
您可以為此嘗試itertools.product
。
import itertools
print(list(itertools.product(*[(True,False)]*7)))
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.