我想在python代码中获得两个数字的排列,最多重复n次。
示例:
a = 10, b = 100 and given n = 3
现在我想要输出:
(10, 10, 10),
(10, 10, 100),
(10, 100, 10),
(10, 100, 100),
(100, 10, 10),
(100, 10, 100),
(100, 100, 10),
(100, 100, 100)
我尝试过itertools排列,但不会有帮助。任何人请给我一个解决方案。
答案 0 :(得分:1)
您可以使用itertools:
from itertools import product
nums = [10, 100]
n = 3
ans = list(product(nums, repeat=n))
print(ans)
答案 1 :(得分:1)
您可以使用itertools.product
并将repeat
设置为3:
from itertools import product
a, b = 10, 100
n = 3
list(product([a,b], repeat=n))
[(10, 10, 10),
(10, 10, 100),
(10, 100, 10),
(10, 100, 100),
(100, 10, 10),
(100, 10, 100),
(100, 100, 10),
(100, 100, 100)]