9

I am not sure that my question is correct but I don't know how to explain it otherwords. So I've got some lists like

a = ['11', '12']
b = ['21', '22']
c = ['31', '32']

And i need to get something like:

result = [
    ['11', '21', '31'],
    ['11', '21', '32'],
    ['11', '22', '31'],
    ['11', '22', '32'],
    ['12', '21', '31'],
    ['12', '21', '32'],
    ['12', '22', '31'],
    ['12', '22', '32']
]
GhostKU
  • 1,898
  • 6
  • 23
  • 32
  • Duplicate: [pick combinations from multiple lists](https://stackoverflow.com/q/15305719/1324033) – Sayse May 25 '17 at 08:42
  • Please try to research your problem yourself first, copying your question title into google produces multiple duplicates. – Sayse May 25 '17 at 08:43
  • Will all of the lists be of length two if so it looks like a binary counting system so you could do that with an algorithm, but the post using itertools is correct. – Alessi 42 May 25 '17 at 08:44

5 Answers5

7

You need itertools.product which returns cartesian product of input iterables.

>>> a = ['11', '12']
>>> b = ['21', '22']
>>> c = ['31', '32']
>>>
>>> from itertools import product
>>>
>>> list(product(a,b,c))
[('11', '21', '31'), ('11', '21', '32'), ('11', '22', '31'), ('11', '22', '32'), ('12', '21', '31'), ('12', '21', '32'), ('12', '22', '31'), ('12', '22', '32')]

And you can use a list comprehension to convert tuples to lists:

>>> [list(i) for i in product(a,b,c)]
[['11', '21', '31'], ['11', '21', '32'], ['11', '22', '31'], ['11', '22', '32'], ['12', '21', '31'], ['12', '21', '32'], ['12', '22', '31'], ['12', '22', '32']]
McGrady
  • 10,869
  • 13
  • 47
  • 69
4

User itertools, combinations:

import itertools
a = ['11', '12']
b = ['21', '22']
c = ['31', '32']
list(itertools.combinations(itertools.chain(a,b,c), 3))
[('11', '12', '21'), ('11', '12', '22'), ('11', '12', '31'), ('11', '12', '32'), ('11', '21', '22'), ('11', '21', '31'), ('11', '21', '32'), ('11', '22', '31'), ('11', '22', '32'), ('11', '31', '32'), ('12', '21', '22'), ('12', '21', '31'), ('12', '21', '32'), ('12', '22', '31'), ('12', '22', '32'), ('12', '31', '32'), ('21', '22', '31'), ('21', '22', '32'), ('21', '31', '32'), ('22', '31', '32')]

or product:

list(itertools.product(a,b,c))
[('11', '21', '31'), ('11', '21', '32'), ('11', '22', '31'), ('11', '22', '32'), ('12', '21', '31'), ('12', '21', '32'), ('12', '22', '31'), ('12', '22', '32')]
Netwave
  • 40,134
  • 6
  • 50
  • 93
0
import itertools
list(itertools.product(a,b,c))

Or using numpy

import numpy
[list(x) for x in numpy.array(numpy.meshgrid(a,b,c)).T.reshape(-1,len(a))]
khelili miliana
  • 3,730
  • 2
  • 15
  • 28
0
import numpy as np    
np.array(np.meshgrid(a, b, c)).T.reshape(-1,3)

edit

import numpy as np 
len = 3 #combination array length
np.array(np.meshgrid(a, b, c)).T.reshape(-1,len)
R.A.Munna
  • 1,699
  • 1
  • 15
  • 29
0

A solution without using any library:

def subproblem(list_a, list_b):
    c = list()
    for i, a_i in enumerate(list_a):
        for j, b_j in enumerate(list_b):
            c.append(a_i + [b_j])
    return c


def multi_lists_find_all_combination(lists):
    com = [[]]
    if len(lists) == 1:
        return lists[0]
    for i in range(0, len(lists)):
        print(i)
        com = subproblem(com, lists[i])
    return com

a = ['11', '12']
b = ['21', '22']
c = ['31', '32']

ans = multi_lists_find_all_combination([a,b,c])

Raymond Pang
  • 667
  • 1
  • 7
  • 17