[英]Create a list of lists of possible combinations
我有一個列表,其中包含以下項目:
initl = [1, 2, 3, 4]
和一個stepsize
,即stepsize = 0.05
。 列表中的每個值都可以按stepsize
向上或向下更改。 我想要創建的是一個列表列表,其中包含向上、向下或初始值的所有組合,例如:
result_list = [[1, 2, 3, 4], [1.05, 2, 3, 4], [0.95, 2, 3, 4], [1, 2.05, 3, 4], ...]
列表中的組合順序無關緊要。
我想出了這個:
import itertools
initl = [1, 2, 3, 4]
stepsize = 0.05
lu = [i + stepsize for i in initl]
ld = [i - stepsize for i in initl]
l_map = list(itertools.product(range(3), repeat=4))
result_list = []
for i in l_map:
l_new = []
for pos, j in enumerate(i):
if j == 0:
l_new.append(ld[pos])
elif j == 1:
l_new.append(initl[pos])
else:
l_new.append(lu[pos])
result_list.append(l_new)
這會產生所需的 output。 列表長度:3 ^ 4 = 81。但我想知道是否有更好的方法。 特別是嵌套的 for 循環對我來說似乎很笨重。 任何幫助表示贊賞。
你有正確的想法,基本上實現numpy.choose
。選擇你自己,對於一個 3 選擇的情況。
您可以直接在包含每個值的所有選項的元組的列表上簡化使用itertools.product
,如下所示:
import itertools
initl = [1, 2, 3, 4]
stepsize = 0.05
prod_seed = [(i, i+stepsize, i-stepsize) for i in initl]
result_list = list(itertools.product(*prod_seed))
print(result_list)
print(len(result_list))
Output:
[(1, 2, 3, 4), (1, 2, 3, 4.05), (1, 2, 3, 3.95), ....]
81
嘗試:
def getComb(arr, step, res=[]):
if(len(arr)==1):
for el in [-step, 0, step]:
yield res+[arr[0]+el]
else:
for el in [-step, 0, step]:
yield from getComb(arr[1:], step, res+[arr[0]+el])
對於您的輸入數據輸出:
>>> for el in getComb(initl, stepsize): print(el)
[0.95, 1.95, 2.95, 3.95]
[0.95, 1.95, 2.95, 4]
[0.95, 1.95, 2.95, 4.05]
[0.95, 1.95, 3, 3.95]
[0.95, 1.95, 3, 4]
...
[1.05, 2.05, 3, 4]
[1.05, 2.05, 3, 4.05]
[1.05, 2.05, 3.05, 3.95]
[1.05, 2.05, 3.05, 4]
[1.05, 2.05, 3.05, 4.05]
您可以嘗試如下的列表理解, zip
使用initl
對product
s 執行操作:
>>> from itertools import product
>>> initl = [1, 2, 3, 4]
>>> step = [-0.05, 0, 0.05]
>>> [[x+d for x, d in zip(initl, p)] for p in product(step, repeat=len(initl))]
[[0.95, 1.95, 2.95, 3.95],
[0.95, 1.95, 2.95, 4],
...
[1.05, 2.05, 3.05, 4.05]]
>>> len(_)
81
您快到了。 您需要做的就是轉置由 3 個(4 個長度)列表組成的矩陣,以便itertools.product可以從[elem[i] - stepsize, elem[i], elem[i] + stepsize]
中選擇每個元素. 這是由zip function 完成的。
>>> import itertools as it >>> >>> initl = [1, 2, 3, 4] >>> stepsize = 0.05 >>> >>> lo = [elem - stepsize for elem in initl] >>> hi = [elem + stepsize for elem in initl] >>> lo, hi ([0.95, 1.95, 2.95, 3.95], [1.05, 2.05, 3.05, 4.05]) >>> >>> list(it.product(*zip(lo, initl, hi))) [(0.95, 1.95, 2.95, 3.95), (0.95, 1.95, 2.95, 4), (0.95, 1.95, 2.95, 4.05), (0.95, 1.95, 3, 3.95), (0.95, 1.95, 3, 4), (0.95, 1.95, 3, 4.05), (0.95, 1.95, 3.05, 3.95), (0.95, 1.95, 3.05, 4), (0.95, 1.95, 3.05, 4.05), (0.95, 2, 2.95, 3.95), (0.95, 2, 2.95, 4), (0.95, 2, 2.95, 4.05), (0.95, 2, 3, 3.95), (0.95, 2, 3, 4), (0.95, 2, 3, 4.05), (0.95, 2, 3.05, 3.95), (0.95, 2, 3.05, 4), (0.95, 2, 3.05, 4.05), (0.95, 2.05, 2.95, 3.95), (0.95, 2.05, 2.95, 4), (0.95, 2.05, 2.95, 4.05), (0.95, 2.05, 3, 3.95), (0.95, 2.05, 3, 4), (0.95, 2.05, 3, 4.05), (0.95, 2.05, 3.05, 3.95), (0.95, 2.05, 3.05, 4), (0.95, 2.05, 3.05, 4.05), (1, 1.95, 2.95, 3.95), (1, 1.95, 2.95, 4), (1, 1.95, 2.95, 4.05), (1, 1.95, 3, 3.95), (1, 1.95, 3, 4), (1, 1.95, 3, 4.05), (1, 1.95, 3.05, 3.95), (1, 1.95, 3.05, 4), (1, 1.95, 3.05, 4.05), (1, 2, 2.95, 3.95), (1, 2, 2.95, 4), (1, 2, 2.95, 4.05), (1, 2, 3, 3.95), (1, 2, 3, 4), (1, 2, 3, 4.05), (1, 2, 3.05, 3.95), (1, 2, 3.05, 4), (1, 2, 3.05, 4.05), (1, 2.05, 2.95, 3.95), (1, 2.05, 2.95, 4), (1, 2.05, 2.95, 4.05), (1, 2.05, 3, 3.95), (1, 2.05, 3, 4), (1, 2.05, 3, 4.05), (1, 2.05, 3.05, 3.95), (1, 2.05, 3.05, 4), (1, 2.05, 3.05, 4.05), (1.05, 1.95, 2.95, 3.95), (1.05, 1.95, 2.95, 4), (1.05, 1.95, 2.95, 4.05), (1.05, 1.95, 3, 3.95), (1.05, 1.95, 3, 4), (1.05, 1.95, 3, 4.05), (1.05, 1.95, 3.05, 3.95), (1.05, 1.95, 3.05, 4), (1.05, 1.95, 3.05, 4.05), (1.05, 2, 2.95, 3.95), (1.05, 2, 2.95, 4), (1.05, 2, 2.95, 4.05), (1.05, 2, 3, 3.95), (1.05, 2, 3, 4), (1.05, 2, 3, 4.05), (1.05, 2, 3.05, 3.95), (1.05, 2, 3.05, 4), (1.05, 2, 3.05, 4.05), (1.05, 2.05, 2.95, 3.95), (1.05, 2.05, 2.95, 4), (1.05, 2.05, 2.95, 4.05), (1.05, 2.05, 3, 3.95), (1.05, 2.05, 3, 4), (1.05, 2.05, 3, 4.05), (1.05, 2.05, 3.05, 3.95), (1.05, 2.05, 3.05, 4), (1.05, 2.05, 3.05, 4.05)]
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.