All combinations of a list of lists
you need itertools.product
:
>>> import itertools
>>> a = [[1,2,3],[4,5,6],[7,8,9,10]]
>>> list(itertools.product(*a))
[(1, 4, 7), (1, 4, 8), (1, 4, 9), (1, 4, 10), (1, 5, 7), (1, 5, 8), (1, 5, 9), (1, 5, 10), (1, 6, 7), (1, 6, 8), (1, 6, 9), (1, 6, 10), (2, 4, 7), (2, 4, 8), (2, 4, 9), (2, 4, 10), (2, 5, 7), (2, 5, 8), (2, 5, 9), (2, 5, 10), (2, 6, 7), (2, 6, 8), (2, 6, 9), (2, 6, 10), (3, 4, 7), (3, 4, 8), (3, 4, 9), (3, 4, 10), (3, 5, 7), (3, 5, 8), (3, 5, 9), (3, 5, 10), (3, 6, 7), (3, 6, 8), (3, 6, 9), (3, 6, 10)]
How to get all combination from multiple lists?
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')]
How to create all possible combinations of a list which has lists inside of it?
Possible solution is the following:
from itertools import combinations
lst = [['Macau', 'United Kingdom', 'India'], ['United States', 'China'], ['China', 'Australia']]
print([list(combinations(group, 2)) for group in lst])
Returns
[[('Macau', 'United Kingdom'),
('Macau', 'India'),
('United Kingdom', 'India')],
[('United States', 'China')],
[('China', 'Australia')]]
How to generate combinations of elements of different lists?
As you asked for a solution without itertools
, this one is a recursive function that takes a list of any length and does the combination you need:
def combine(elems):
if len(elems) == 0:
return [[]]
result = []
subcombinations = combine(elems[1:])
for x in elems[0]:
for y in subcombinations:
result.append([x, *y])
return result
Or a much shorter version
def combine(elems):
if len(elems) == 0:
return [[]]
return [[x, *y] for x in elems[0] for y in combine(elems[1:])]
Python all combinations of a list of lists
You can use itertools.chain.from_iterable
to flatten the tuple of lists into a list. Example -
import itertools
input = [['a','b'],['c','d'],['e','f']]
combs = []
for i in xrange(1, len(input)+1):
els = [list(itertools.chain.from_iterable(x)) for x in itertools.combinations(input, i)]
combs.extend(els)
Demo -
>>> import itertools
>>> input = [['a','b'],['c','d'],['e','f']]
>>> combs = []
>>> for i in range(1, len(input)+1):
... els = [list(itertools.chain.from_iterable(x)) for x in itertools.combinations(input, i)]
... combs.extend(els)
...
>>> import pprint
>>> pprint.pprint(combs)
[['a', 'b'],
['c', 'd'],
['e', 'f'],
['a', 'b', 'c', 'd'],
['a', 'b', 'e', 'f'],
['c', 'd', 'e', 'f'],
['a', 'b', 'c', 'd', 'e', 'f']]
Get all possible combinations from a list of lists by picking one item from each list
Use itertools.product
:
>>> l = [[1,2],[3],[4,5,6],[7],[8,9]]
>>> from itertools import product
>>> list(product(*l))
[(1, 3, 4, 7, 8), (1, 3, 4, 7, 9), (1, 3, 5, 7, 8), (1, 3, 5, 7, 9), (1, 3, 6, 7, 8), (1, 3, 6, 7, 9), (2, 3, 4, 7, 8), (2, 3, 4, 7, 9), (2, 3, 5, 7, 8), (2, 3, 5, 7, 9), (2, 3, 6, 7, 8), (2, 3, 6, 7, 9)]
all combination in list of lists without duplicates in python
1) itertools.product
all_combinations = itertools.product(elements)
2) filter
with lambda
filtered_combinations = filter(lambda x: len(x) != len(set(x)), all_combinations)
Related Topics
How to Call a Script from Another Script
How to Check If a List Is Empty
How to Properly Determine the Current Script Directory
Equivalent of Shell 'Cd' Command to Change the Working Directory
Is There Any Pythonic Way to Combine Two Dicts (Adding Values For Keys That Appear in Both)
Difference Between Del, Remove, and Pop on Lists
Changing One List Unexpectedly Changes Another, Too
Groupby Value Counts on the Dataframe Pandas
Which Python Memory Profiler Is Recommended
What's the U Prefix in a Python String
How Do Python'S Any and All Functions Work
What Is Getattr() Exactly and How to Use It
Save Plot to Image File Instead of Displaying It Using Matplotlib