Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python combinations without repetitions

Tags:

I have a list of numbers and I want to make combinations from it. If I have list:

t = [2,2,2,2,4] c = list(itertools.combinations(t, 4)) 

The result is:

(2, 2, 2, 2) (2, 2, 2, 4) (2, 2, 2, 4) (2, 2, 2, 4) (2, 2, 2, 4) 

but I want to get:

(2, 2, 2, 2) (2, 2, 2, 4) 

Is it possible to eliminate duplicates except making new list and going through first list?

like image 963
GoobyPrs Avatar asked Apr 05 '16 14:04

GoobyPrs


People also ask

How do you find all possible combinations without repetition in Python?

A. To create combinations without using itertools, iterate the list one by one and fix the first element of the list and make combinations with the remaining list. Similarly, iterate with all the list elements one by one by recursion of the remaining list.

How do you find possible combinations in Python?

To calculate the combinations of a dictionary in Python, use the itertools. combinations() method. The combinations() method takes a dictionary as an argument and returns all the possible combinations of the dictionary elements.

How do you generate all possible combinations of two lists in Python?

The unique combination of two lists in Python can be formed by pairing each element of the first list with the elements of the second list. Method 1 : Using permutation() of itertools package and zip() function. Approach : Import itertools package and initialize list_1 and list_2.


2 Answers

As Donkey Kong points to set, You can get the unique values in a list by converting the list to a set :

t = [2,2,2,2,4] c = list(itertools.combinations(t, 4)) unq = set(c) print(unq) 

And the result will be:

{(2, 2, 2, 4), (2, 2, 2, 2)} 

If you want to use it as a list, you can convert it back by doing :

result = list(unq) 

Alternative and more clean,comprehensive way will be :

t = [2,2,2,2,4] c = set(itertools.combinations(t, 4)) 
like image 139
Randhawa Avatar answered Sep 21 '22 00:09

Randhawa


I know this is late but I want to add a point.

set(itertools.combinations(t, 4)) would do a fine job for most cases, but it still iterates all repetitive combinations internally and so it can be computationally heavy. This is especially the case if there aren't many actual unique combinations.

This one iterates only unique combinations:

from itertools import chain, repeat, count, islice from collections import Counter   def repeat_chain(values, counts):     return chain.from_iterable(map(repeat, values, counts))   def unique_combinations_from_value_counts(values, counts, r):     n = len(counts)     indices = list(islice(repeat_chain(count(), counts), r))     if len(indices) < r:         return     while True:         yield tuple(values[i] for i in indices)         for i, j in zip(reversed(range(r)), repeat_chain(reversed(range(n)), reversed(counts))):             if indices[i] != j:                 break         else:             return         j = indices[i] + 1         for i, j in zip(range(i, r), repeat_chain(count(j), counts[j:])):             indices[i] = j   def unique_combinations(iterable, r):     values, counts = zip(*Counter(iterable).items())     return unique_combinations_from_value_counts(values, counts, r) 

Usage:

>>> list(unique_combinations([2, 2, 2, 2, 4], 4)) # elements must be hashable [(2, 2, 2, 2), (2, 2, 2, 4)]  # You can pass values and counts separately. For this usage, values don't need to be hashable # Say you have ['a','b','b','c','c','c'], then since there is 1 of 'a', 2 of 'b', and 3 of 'c', you can do as follows: >>> list(unique_combinations_from_value_counts(['a', 'b', 'c'], [1, 2, 3], 3)) [('a', 'b', 'b'), ('a', 'b', 'c'), ('a', 'c', 'c'), ('b', 'b', 'c'), ('b', 'c', 'c'), ('c', 'c', 'c')]  # combinations_without_repetition() is a generator (and thus an iterator) # so you can iterate it >>> for comb in unique_combinations([2, 2, 2, 2, 4], 4): ...     print(sum(comb)) ... 8   # 2+2+2+2 10  # 2+2+2+4 

Note that itertools.combinations() is implemented in C, which means it is much faster than my python script for most cases. This code works better than set(itertools.combinations()) method only when there are A LOT MORE repetitive combinations than unique combinations.

like image 40
hahho Avatar answered Sep 20 '22 00:09

hahho