Python merging two lists with all possible permutations

后端 未结 7 1775
醉话见心
醉话见心 2020-12-02 15:09

I\'m trying to figure out the best way to merge two lists into all possible combinations. So, if I start with two lists like this:

list1 = [1, 2]
list2 = [3,         


        
7条回答
  •  北海茫月
    2020-12-02 15:28

    You can create a list by constructing all the permutations of two list members with this, containing the list combinations.

    lst1 = [1,2]
    lst2 = [3,4]
    
    #lst = [[j,k] for j in lst1 for k in lst2] # [[1,3],[1,4],[2,3],[2,4]]
    lst = [[[j,k] for j in lst1] for k in lst2] # [[[1,3],[2,3]],[[1,4],[2,4]]]
    print lst
    

提交回复
热议问题