Search code examples
pythonpython-itertools

Combinations between values of lists


Lets say I am having the following lists:

letters = ['a','b','c']
numbers = ['one','two']
others = ['here','there']

And I want all the possible compinations of all the values. I am doing the following:

from itertools import permutations
b = []
b.extend(letters)
b.extend(numbers)
b.extend(others)
result1 = list(permutations(b,2))

The results are ok. But what I want more specifically is to know the 'types' of compinations. For example result should be:

('a','b','letters-letters')

or

('a','one','letters-numbers')
('one','a','letters-numbers')

I am using the following code:

from itertools import product
result2=[]
result2.extend([x+('letter-letter',) for x in list(permutations(letters ,2))])
result2.extend([x+('number-number',) for x in list(permutations(numbers,2))])
result2.extend([x+('other-other',) for x in list(permutations(others,2))])
result2.extend([x+('number-letter',) for x in list(product(numbers,letters))])
result2.extend([x+('number-letter',) for x in list(product(letters,numbers))])   
result2.extend([x+('number-others',) for x in list(product(numbers,others))])  
result2.extend([x+('number-others',) for x in list(product(others,numbers))])   
result2.extend([x+('letters-others',) for x in list(product(letters,others))])    
result2.extend([x+('letters-others',) for x in list(product(others,letters))])   

Is there a more quick,elegant way of doing this?


Solution

  • IIUC, the key is coupling which data series an element is a member of to the element. For example:

    from itertools import permutations
    
    data = {'letters': ['a','b','c'],
            'numbers': ['one','two'],
            'others': ['here','there']}
    
    poss = [(v,k) for k, vv in data.items() for v in vv]
    results = (list(zip(*p)) for p in permutations(poss, 2))
    results = [p[0] + ('-'.join(p[1]),) for p in results]
    

    gives me

    [('a', 'b', 'letters-letters'),
     ('a', 'c', 'letters-letters'),
     ('a', 'here', 'letters-others'),
     ...
     ('two', 'here', 'numbers-others'),
     ('two', 'there', 'numbers-others'),
     ('two', 'one', 'numbers-numbers')]
    

    which works because we start with a poss looking like

    >>> poss[:3]
    [('a', 'letters'), ('b', 'letters'), ('c', 'letters')]
    

    and then select two elements from it, using the zip-star to turn each selected pair into something like

    >>> next(list(zip(*p)) for p in permutations(poss, 2))
    [('a', 'b'), ('letters', 'letters')]