Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cartesian products of lists without duplicates

Tags:

python

numpy

Given an array a=['a','b','c'], how would you go about returning the Cartesian product of the array without duplicates. Example:

[['a', 'a' , 'a' ,'a']
['a', 'a' , 'a' ,'b']
['a', 'a' , 'a' ,'c']
['a', 'a' , 'b' ,'b']
['a', 'a' , 'b' ,'c']
['a', 'a' , 'c' ,'c']
...etc..]

Following How to generate all permutations of a list in Python, I tried :

print list(itertools.permutations(['a', 'b' , 'c'], 4))
[]

print list(itertools.product(['a', 'b' , 'c'], repeat=4)

But I get the Cartesian product with duplicates. For example the list will contain both ['a','a','b','b'] and ['a','b','b','a'] which are clearly the equal.

Note: my 'a','b','c' are variables which store numbers say 1,2,3. So after getting the list of combinations of the letters, I would need to: say,

['a','b','c','c'] ----> a*b*c*c = 1*2*3*3 = 18

What is the fastest way of doing this in python? Would it be possible/faster to do it with numpy?? Thanks!

like image 229
Oniropolo Avatar asked May 07 '13 15:05

Oniropolo


People also ask

Do Cartesian products have duplicates?

Cartesian product does not have existence without set. And, a set cant have duplicate elements. So there is no chance of getting a duplicate in case of cartesian product.

What is the Cartesian product of 3 sets?

Note: A × A × A = {(a, b, c) : a, b, c ∈ A}.

How do you create a Cartesian product of two lists in Python?

Practical Data Science using Python As we know if two lists are like (a, b) and (c, d) then the Cartesian product will be {(a, c), (a, d), (b, c), (b, d)}. To do this we shall use itertools library and use the product() function present in this library. The returned value of this function is an iterator.

How do you write a Cartesian product in Python?

Introducing The Cartesian Product / Cross Product Of A Set The elements (a,b) are ordered pairs. For example if A = {1,2} and B = {4,5,6} then the cartesian products of A and B is AxB = {(1,4),(1,5),(1,6),(2,4),(2,5),(2,6)}.


1 Answers

Maybe you actually want combinations_with_replacement?

>>> from itertools import combinations_with_replacement
>>> a = ['a', 'b', 'c']
>>> c = combinations_with_replacement(a, 4)
>>> for x in c:
...     print x
...     
('a', 'a', 'a', 'a')
('a', 'a', 'a', 'b')
('a', 'a', 'a', 'c')
('a', 'a', 'b', 'b')
('a', 'a', 'b', 'c')
('a', 'a', 'c', 'c')
('a', 'b', 'b', 'b')
('a', 'b', 'b', 'c')
('a', 'b', 'c', 'c')
('a', 'c', 'c', 'c')
('b', 'b', 'b', 'b')
('b', 'b', 'b', 'c')
('b', 'b', 'c', 'c')
('b', 'c', 'c', 'c')
('c', 'c', 'c', 'c')

Without more information about how you're mapping strings to numbers I can't comment on your second question, but writing your own product function or using numpy's isn't too difficult.

like image 116
DSM Avatar answered Oct 23 '22 21:10

DSM