Python merging two lists with all possible permutations

后端 未结 7 1774
醉话见心
醉话见心 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:23

    The accepted answer can be simplified to

    a = [1, 2, 3]
    b = [4, 5, 6]
    [list(zip(a, p)) for p in permutations(b)]
    

    (The list() call can be omitted in Python 2)

提交回复
热议问题