2 回答

TA貢獻1794條經(jīng)驗 獲得超8個贊
寫一個包裝器。
def wrapper_combinations_with_replacement(iterable, r):
comb = combinations_with_replacement(iterable, r)
for item in comb:
yield list(item)
現(xiàn)在你有了一個列表的列表。
a = 1
b = 2
c = 3
d = 4
comb = wrapper_combinations_with_replacement([a, b, c, d], 2)
for i in list(comb):
print(i)
結(jié)果是:
[1, 1]
[1, 2]
[1, 3]
[1, 4]
[2, 2]
[2, 3]
[2, 4]
[3, 3]
[3, 4]
[4, 4]
或者使用list
list(wrapper_combinations_with_replacement([a, b, c, d], 2))
結(jié)果:
[[1, 1],
[1, 2],
[1, 3],
[1, 4],
[2, 2],
[2, 3],
[2, 4],
[3, 3],
[3, 4],
[4, 4]]

TA貢獻1815條經(jīng)驗 獲得超10個贊
comb_list = [list(combination) for combination in (list(comb))]
這將為您提供一個數(shù)組形式的組合列表
for array_combination in comb_list:
print(array_combination)
Output:
> [1, 1]
[1, 2]
[1, 3]
[1, 4]
[2, 2]
[2, 3]
[2, 4]
[3, 3]
[3, 4]
[4, 4]
添加回答
舉報