The pythonic way to generate pairs

前端 未结 7 1049
轮回少年
轮回少年 2020-12-30 20:00

I want something like code below, but \"pythonic\" style or using standard library:

def combinations(a,b):
    for i in a:
        for j in b:
             y         


        
7条回答
  •  余生分开走
    2020-12-30 20:35

    Create set of pairs (even,odd) combination

    >>> a = { (i,j) for i in range(0,10,2) for j in range(1,10,2)}  
    >>> a
    {(4, 7), (6, 9), (0, 7), (2, 1), (8, 9), (0, 3), (2, 5), (8, 5), (4, 9), (6, 7), (2, 9), (8, 1), (6, 3), (4, 1), (4, 5), (0, 5), (2, 3), (8, 7), (6, 5), (0, 1), (2, 7), (8, 3), (6, 1), (4, 3), (0, 9)}
    
    def combinations(lista, listb):
        return { (i,j) for i in lista for j in listb }
    
    >>> combinations([1,3,5,6],[11,21,133,134,443])
    {(1, 21), (5, 133), (5, 11), (5, 134), (6, 11), (6, 134), (1, 443), (3, 11), (6, 21), (3, 21), (1, 133), (1, 134), (5, 21), (3, 134), (5, 443), (6, 443), (1, 11), (3, 443), (6, 133), (3, 133)}
    

提交回复
热议问题