All combinations of a list of lists

后端 未结 7 1467
离开以前
离开以前 2020-11-22 00:38

I\'m basically looking for a python version of Combination of List>

Given a list of lists, I need a new list that gives all the possible combin

相关标签:
7条回答
  • 2020-11-22 01:03

    The most elegant solution is to use itertools.product in python 2.6.

    If you aren't using Python 2.6, the docs for itertools.product actually show an equivalent function to do the product the "manual" way:

    def product(*args, **kwds):
        # product('ABCD', 'xy') --> Ax Ay Bx By Cx Cy Dx Dy
        # product(range(2), repeat=3) --> 000 001 010 011 100 101 110 111
        pools = map(tuple, args) * kwds.get('repeat', 1)
        result = [[]]
        for pool in pools:
            result = [x+[y] for x in result for y in pool]
        for prod in result:
            yield tuple(prod)
    
    0 讨论(0)
提交回复
热议问题