我有 numpy 數組,每個數組包含 2 個浮點數:Unit_1 = [40000.0, 47000.0]
Unit_2 = [12000.0, 14000.0]
Unit_3 = [16000.0, 18000.0]我想根據單位的值構建單位組合的排列,例如:[(40000, 12000, 16000), (40000, 12000, 18000), (40000, 14000, 160000), (40000, 14000, 18000).........]得到所有可能的排列。我正在使用以下內容:list(list(zip(r, p, q)) for (r, p,q) in zip(repeat(Unit_1), permutations(Unit_2), permutations(Unit_3)))它給出的輸出是:[[(40000.0, 12000.0, 16000.0), (47000.0, 14000.0, 18000.0)], [(40000.0, 14000.0, 18000.0), (47000.0, 12000.0, 16000.0)]]我怎樣才能得到剩余的排列?
1 回答

胡子哥哥
TA貢獻1825條經驗 獲得超6個贊
您似乎不需要排列,而是需要3 個列表的笛卡爾積。使用itertools.product
.
items = list(itertools.product(Unit_1, Unit_2, Unit_3))
要生成排列,您需要上述集合中每個項目的排列。所以,像
perms = sum((list(permutations(item)) for item in items), [])
添加回答
舉報
0/150
提交
取消