The pythonic way to generate pairs

前端 未结 7 1042
轮回少年
轮回少年 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
    

提交回复
热议问题