Commutative combination of elements of two lists

前端 未结 6 563
感动是毒
感动是毒 2021-01-15 14:13

Just a style question: Is there a build-in method to get the combinations under the assertion of commutative property and excluding elements paired with itself?



        
6条回答
  •  生来不讨喜
    2021-01-15 14:28

    you mean this?

    a = ["1", "2", "3"]
    b = ["1", "2", "3"]
    print [(x,y) for x in a for y in b]
    

    output:

    [('1', '1'), ('1', '2'), ('1', '3'), ('2', '1'), ('2', '2'), ('2', '3'), ('3', '1'), ('3', '2'), ('3', '3')]
    

提交回复
热议问题