mindspore.ops.combinations
- mindspore.ops.combinations(input, r=2, with_replacement=False)[源代码]
返回输入tensor中所有长度为 r 的子序列。
当 with_replacement 为
False
时,功能与Python的 itertools.combinations 类似;当为True
时,功能与 itertools.combinations_with_replacement 一致。- 参数:
input (Tensor) - 一维输入tensor。
r (int,可选) - 进行组合的子序列长度。默认
2
。with_replacement (bool,可选) - 是否允许组合存在重复值。默认
False
。
- 返回:
Tensor
- 支持平台:
Ascend
GPU
CPU
样例:
>>> import mindspore >>> input = mindspore.tensor(input) >>> mindspore.ops.combinations(input) Tensor(shape=[3, 2], dtype=Int64, value= [[1, 2], [1, 3], [2, 3]]) >>> mindspore.ops.combinations(input, r=3) Tensor(shape=[1, 3], dtype=Int64, value= [[1, 2, 3]]) >>> mindspore.ops.combinations(input, with_replacement=True) Tensor(shape=[6, 2], dtype=Int64, value= [[1, 1], [1, 2], [1, 3], [2, 2], [2, 3], [3, 3]]) >>> # It has the same results as using itertools.combinations. >>> import itertools >>> input = [1, 2, 3] >>> list(itertools.combinations(input, r=2)) [(1, 2), (1, 3), (2, 3)] >>> list(itertools.combinations(input, r=3)) [(1, 2, 3)] >>> list(itertools.combinations_with_replacement(input, r=2)) [(1, 1), (1, 2), (1, 3), (2, 2), (2, 3), (3, 3)]