【问题标题】:Get combinations of array without self repetition获取不重复的数组组合
【发布时间】:2021-02-16 20:24:23
【问题描述】:

我有以下形状为(3,3) 的数组points,它们是3D 点

[[-0.20592405  0.33034652  1.0430001 ]
 [-0.23757319  0.29612657  1.03800011]
 [-0.304515    0.34571081  1.20400012]]

我需要找出这三点的所有组合。我正在执行以下操作:

comb = np.array(list(itertools.product(points.tolist(), repeat=3)))

我得到了

[[[-0.20592405  0.33034652  1.0430001 ]
  [-0.20592405  0.33034652  1.0430001 ]
  [-0.20592405  0.33034652  1.0430001 ]]

 [[-0.20592405  0.33034652  1.0430001 ]
  [-0.20592405  0.33034652  1.0430001 ]
  [-0.23757319  0.29612657  1.03800011]]

 [[-0.20592405  0.33034652  1.0430001 ]
  [-0.20592405  0.33034652  1.0430001 ]
  [-0.304515    0.34571081  1.20400012]]

 [[-0.20592405  0.33034652  1.0430001 ]
  [-0.23757319  0.29612657  1.03800011]
  [-0.20592405  0.33034652  1.0430001 ]]

 [[-0.20592405  0.33034652  1.0430001 ]
  [-0.23757319  0.29612657  1.03800011]
  [-0.23757319  0.29612657  1.03800011]]

 [[-0.20592405  0.33034652  1.0430001 ]
  [-0.23757319  0.29612657  1.03800011]
  [-0.304515    0.34571081  1.20400012]]

 [[-0.20592405  0.33034652  1.0430001 ]
  [-0.304515    0.34571081  1.20400012]
  [-0.20592405  0.33034652  1.0430001 ]]

 [[-0.20592405  0.33034652  1.0430001 ]
  [-0.304515    0.34571081  1.20400012]
  [-0.23757319  0.29612657  1.03800011]]

 [[-0.20592405  0.33034652  1.0430001 ]
  [-0.304515    0.34571081  1.20400012]
  [-0.304515    0.34571081  1.20400012]]

 [[-0.23757319  0.29612657  1.03800011]
  [-0.20592405  0.33034652  1.0430001 ]
  [-0.20592405  0.33034652  1.0430001 ]]

 [[-0.23757319  0.29612657  1.03800011]
  [-0.20592405  0.33034652  1.0430001 ]
  [-0.23757319  0.29612657  1.03800011]]

 [[-0.23757319  0.29612657  1.03800011]
  [-0.20592405  0.33034652  1.0430001 ]
  [-0.304515    0.34571081  1.20400012]]

 [[-0.23757319  0.29612657  1.03800011]
  [-0.23757319  0.29612657  1.03800011]
  [-0.20592405  0.33034652  1.0430001 ]]

 [[-0.23757319  0.29612657  1.03800011]
  [-0.23757319  0.29612657  1.03800011]
  [-0.23757319  0.29612657  1.03800011]]

 [[-0.23757319  0.29612657  1.03800011]
  [-0.23757319  0.29612657  1.03800011]
  [-0.304515    0.34571081  1.20400012]]

 [[-0.23757319  0.29612657  1.03800011]
  [-0.304515    0.34571081  1.20400012]
  [-0.20592405  0.33034652  1.0430001 ]]

 [[-0.23757319  0.29612657  1.03800011]
  [-0.304515    0.34571081  1.20400012]
  [-0.23757319  0.29612657  1.03800011]]

 [[-0.23757319  0.29612657  1.03800011]
  [-0.304515    0.34571081  1.20400012]
  [-0.304515    0.34571081  1.20400012]]

 [[-0.304515    0.34571081  1.20400012]
  [-0.20592405  0.33034652  1.0430001 ]
  [-0.20592405  0.33034652  1.0430001 ]]

 [[-0.304515    0.34571081  1.20400012]
  [-0.20592405  0.33034652  1.0430001 ]
  [-0.23757319  0.29612657  1.03800011]]

 [[-0.304515    0.34571081  1.20400012]
  [-0.20592405  0.33034652  1.0430001 ]
  [-0.304515    0.34571081  1.20400012]]

 [[-0.304515    0.34571081  1.20400012]
  [-0.23757319  0.29612657  1.03800011]
  [-0.20592405  0.33034652  1.0430001 ]]

 [[-0.304515    0.34571081  1.20400012]
  [-0.23757319  0.29612657  1.03800011]
  [-0.23757319  0.29612657  1.03800011]]

 [[-0.304515    0.34571081  1.20400012]
  [-0.23757319  0.29612657  1.03800011]
  [-0.304515    0.34571081  1.20400012]]

 [[-0.304515    0.34571081  1.20400012]
  [-0.304515    0.34571081  1.20400012]
  [-0.20592405  0.33034652  1.0430001 ]]

 [[-0.304515    0.34571081  1.20400012]
  [-0.304515    0.34571081  1.20400012]
  [-0.23757319  0.29612657  1.03800011]]

 [[-0.304515    0.34571081  1.20400012]
  [-0.304515    0.34571081  1.20400012]
  [-0.304515    0.34571081  1.20400012]]]

这给了我所有的组合,但我想丢弃重复的组合。所以,假设 3D 点 abc 我得到 [[a, a, a], [a, a, b],..,[a, b, c],.., [c, c, c]] 的组合。我只想保留具有所有不同点的组合,即[a, b, c][b, c, a] 等。

【问题讨论】:

  • combinations代替product?
  • 排列应该做:np.array(list(itertools.permutations(points, r=3)))
  • @QuangHoang 你是对的,那行得通!如果您将此作为答案发布,我会接受。
  • @MustafaAydın 我试图避免重复点,这意味着顺序无关紧要。

标签: python numpy combinations itertools


【解决方案1】:

itertools.combinations,只有一个:

l1=[[-0.20592405,  0.33034652,  1.0430001 ],
    [-0.23757319,  0.29612657,  1.03800011],
    [-0.304515,    0.34571081,  1.20400012]]
comb = np.array(list(itertools.combinations(l1, 3)))
#array([[[-0.20592405,  0.33034652,  1.0430001 ],
#        [-0.23757319,  0.29612657,  1.03800011],
#        [-0.304515  ,  0.34571081,  1.20400012]]])

itertools.permutations:

comb = np.array(list(itertools.permutations(l1, 3)))

comb 的值:

array([[[-0.20592405,  0.33034652,  1.0430001 ],
        [-0.23757319,  0.29612657,  1.03800011],
        [-0.304515  ,  0.34571081,  1.20400012]],

       [[-0.20592405,  0.33034652,  1.0430001 ],
        [-0.304515  ,  0.34571081,  1.20400012],
        [-0.23757319,  0.29612657,  1.03800011]],

       [[-0.23757319,  0.29612657,  1.03800011],
        [-0.20592405,  0.33034652,  1.0430001 ],
        [-0.304515  ,  0.34571081,  1.20400012]],

       [[-0.23757319,  0.29612657,  1.03800011],
        [-0.304515  ,  0.34571081,  1.20400012],
        [-0.20592405,  0.33034652,  1.0430001 ]],

       [[-0.304515  ,  0.34571081,  1.20400012],
        [-0.20592405,  0.33034652,  1.0430001 ],
        [-0.23757319,  0.29612657,  1.03800011]],

       [[-0.304515  ,  0.34571081,  1.20400012],
        [-0.23757319,  0.29612657,  1.03800011],
        [-0.20592405,  0.33034652,  1.0430001 ]]])

【讨论】:

    猜你喜欢
    • 2014-03-14
    • 1970-01-01
    • 1970-01-01
    • 2023-04-05
    • 1970-01-01
    • 1970-01-01
    • 2017-09-18
    • 1970-01-01
    • 2021-06-10
    相关资源
    最近更新 更多