2 回答
TA贡献1794条经验 获得超7个赞
写一个包装器。
def wrapper_combinations_with_replacement(iterable, r):
comb = combinations_with_replacement(iterable, r)
for item in comb:
yield list(item)
现在你有了一个列表的列表。
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)
结果是:
[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))
结果:
[[1, 1],
[1, 2],
[1, 3],
[1, 4],
[2, 2],
[2, 3],
[2, 4],
[3, 3],
[3, 4],
[4, 4]]
TA贡献1815条经验 获得超10个赞
comb_list = [list(combination) for combination in (list(comb))]
这将为您提供一个数组形式的组合列表
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]
添加回答
举报