使用迭代器从 [1, 2, 3] 中获取特定的列表序列
using Iterators to get a particular sequence of list from [1, 2, 3]
我想实现这个输出
[(1, 1, 1), (1, 1, 2), (1, 1, 3), (1, 2, 1), (1, 2, 2), (1, 2, 3), (1, 3, 1), (1, 3, 2), (1, 3, 3), (2, 1, 1), (2, 1, 2), (2, 1, 3), (2, 2, 1), (2, 2, 2), (2, 2, 3), (2, 3, 1), (2, 3, 2), (2, 3, 3), (3, 1, 1), (3, 1, 2), (3, 1, 3), (3, 2, 1), (3, 2, 2), (3, 2, 3), (3, 3, 1), (3, 3, 2), (3, 3, 3)]
来自这个给定的列表:[1,2,3]
。
我试过使用
foo = [1, 2, 2]
print(list(permutations(foo, 3)))
还有这个
l
ist(it.combinations_with_replacement([1,2,3], 3))
from itertools import combinations
def rSubset(arr, r):
return list(combinations(arr, r))
if __name__ == "__main__":
arr = [1, 2, 3]
r = 3
print (rSubset(arr, r))
但我无法获得所需的结果,请提供任何帮助
您想要产品,而不是排列或组合。
from itertools import product
print(list(product([1,2,3], repeat=3)))
我想实现这个输出
[(1, 1, 1), (1, 1, 2), (1, 1, 3), (1, 2, 1), (1, 2, 2), (1, 2, 3), (1, 3, 1), (1, 3, 2), (1, 3, 3), (2, 1, 1), (2, 1, 2), (2, 1, 3), (2, 2, 1), (2, 2, 2), (2, 2, 3), (2, 3, 1), (2, 3, 2), (2, 3, 3), (3, 1, 1), (3, 1, 2), (3, 1, 3), (3, 2, 1), (3, 2, 2), (3, 2, 3), (3, 3, 1), (3, 3, 2), (3, 3, 3)]
来自这个给定的列表:[1,2,3]
。
我试过使用
foo = [1, 2, 2]
print(list(permutations(foo, 3)))
还有这个
l
ist(it.combinations_with_replacement([1,2,3], 3))
from itertools import combinations
def rSubset(arr, r):
return list(combinations(arr, r))
if __name__ == "__main__":
arr = [1, 2, 3]
r = 3
print (rSubset(arr, r))
但我无法获得所需的结果,请提供任何帮助
您想要产品,而不是排列或组合。
from itertools import product
print(list(product([1,2,3], repeat=3)))