The pythonic way to generate pairs

前端 未结 7 1017
轮回少年
轮回少年 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 21:01

    These are not really "combinations" in the sense of combinatorics, these are rather elements from the cartesian product of a and b. The function in the standard library to generate these pairs is itertools.product():

    for i, j in itertools.product(a, b):
        # whatever
    
    0 讨论(0)
提交回复
热议问题