【问题标题】:How could I generate a combinatorial of the lines of an array with several columns?如何生成具有多列的数组行的组合?
【发布时间】:2019-10-14 12:58:50
【问题描述】:

我想获取数组中 n 行的所有可能组合。 我不在乎它们是什么顺序,所以这不是一个排列。

示例:

我有一个数组:

[(1,2,3), (4,5,6), (7,8,9)]

问题: 我想找到两行的所有组合:

[(1,2,3), (4,5,6)]
[(1,2,3), (7,8,9)]
…

非常感谢!!

【问题讨论】:

    标签: python-3.x arraylist combinations method-combination


    【解决方案1】:

    使用itertools.combinations 并将组合转换为列表,请参阅the docs

    from itertools import combinations
    
    lst = [(1,2,3), (4,5,6), (7,8,9)]
    for x in combinations(lst, 2):
        print(list(x))
    

    【讨论】:

      【解决方案2】:

      结合 itertools 试试这个

      import itertools
      test = np.array([[1,2,3],[4,5,6],[7,8,9],[10,11,12]])
      for i in range(0, len(test)):
          for j in sorted(np.arange(len(test)-1,i,-1)):
              print(test[i])
              print(test[j])
              print(list(itertools.product(test[i], test[j])))
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多