Cross product of sets using recursion

前端 未结 2 766
挽巷
挽巷 2020-12-06 19:01

I wrote the following recursive routine to compute cross product of two sets.

def combine(input1,input2,output):
    if len(input2)==0:
        return output         


        
2条回答
  •  醉话见心
    2020-12-06 19:35

    Use itertools

    import itertools
    
    print list(itertools.product(input1, input2))
    

    Semantically this is equivalent to:

    for i_1 in input_1:
        for i_2 in input_2:
            for i_3 in input_3:
                ...
                    for i_n in input_n:
                        #do something with i_1, i_2, i_3, ..., i_n
    

    Where n is the number of arguments you pass to product.

提交回复
热议问题