Get all possible combinations from a list with duplicate elements?


username

For example, I have a list of repeating elements array = [2,2,2,7].

If I use the solution suggested in this answer (use ), I get:itertools.combinations()

()
(7,)
(2,)
(2,)
(2,)
(7, 2)
(7, 2)
(7, 2)
(2, 2)
(2, 2)
(2, 2)
(7, 2, 2)
(7, 2, 2)
(7, 2, 2)
(2, 2, 2)
(7, 2, 2, 2)

As you can see, some "combinations" (7,2,2)appear repeatedly , say 3 times.

My desired output is:

()
(7,)
(2,)
(7, 2)
(2, 2)
(7, 2, 2)
(2, 2, 2)
(7, 2, 2, 2)

I can check the output for duplicate combinations, but I feel like that's not the best way to solve this.

bank

You can select a set of combinations and link them together.

from itertools import chain, combinations

arr = [2, 2, 2, 7]

list(chain.from_iterable(set(combinations(arr, i)) for i in range(len(arr) + 1)))
# [(), (7,), (2,), (2, 7), (2, 2), (2, 2, 2), (2, 2, 7), (2, 2, 2, 7)]

Related


get all possible combinations of k elements from a list

Tobias Herman I need a function that does the same thing as itertools.combinations(iterable, r) in python So far I came up with this: {-| forward application -} x -: f = f x infixl 0 -: {-| combinations 2 "ABCD" = ["AB","AC","AD","BC","BD","CD"] -} combinatio

Get all possible combinations of k elements from a list

Tobias Herman I need a function that does the same thing as in pythonitertools.combinations(iterable, r) So far I have come up with this: {-| forward application -} x -: f = f x infixl 0 -: {-| combinations 2 "ABCD" = ["AB","AC","AD","BC","BD","CD"] -} combin

How to get all possible combinations of list elements?

Ben : I have a list of 15 numbers and I need to write some code to generate all 32,768 combinations of these numbers. I've found some code (by googling) that apparently does what I want, but I've found the code to be rather opaque and have been cautious about

How to get all possible combinations of list elements?

Ben : I have a list of 15 numbers and I need to write some code to generate all 32,768 combinations of these numbers. I've found some code (by googling) that apparently does what I want, but I've found the code to be rather opaque and have been cautious about

How to get all possible combinations of list elements?

Ben : I have a list of 15 numbers and I need to write some code to generate all 32,768 combinations of these numbers. I've found some code (by googling) that apparently does what I want, but I've found the code to be rather opaque and have been cautious about

Get all possible combinations of elements

sa555 How would you get all possible combinations of 2 elements in an array? E.g: [ 1, 2, 3, 4 ] becomes [ [1, 2], [1, 3], [1, 4], [2, 1], [2, 3], [2, 4], [3, 1], [3, 2], [3, 4], [4, 1], [4

Get all possible combinations of elements

sa555 How would you get all possible combinations of 2 elements in an array? E.g: [ 1, 2, 3, 4 ] becomes [ [1, 2], [1, 3], [1, 4], [2, 1], [2, 3], [2, 4], [3, 1], [3, 2], [3, 4], [4, 1], [4

Get all possible combinations of elements

sa555 How would you get all possible combinations of 2 elements in an array? E.g: [ 1, 2, 3, 4 ] becomes [ [1, 2], [1, 3], [1, 4], [2, 1], [2, 3], [2, 4], [3, 1], [3, 2], [3, 4], [4, 1], [4

Get all possible combinations of elements

sa555 How would you get all possible combinations of 2 elements in an array? E.g: [ 1, 2, 3, 4 ] becomes [ [1, 2], [1, 3], [1, 4], [2, 1], [2, 3], [2, 4], [3, 1], [3, 2], [3, 4], [4, 1], [4

Get all possible combinations of elements

sa555 How would you get all possible combinations of 2 elements in an array? E.g: [ 1, 2, 3, 4 ] becomes [ [1, 2], [1, 3], [1, 4], [2, 1], [2, 3], [2, 4], [3, 1], [3, 2], [3, 4], [4, 1], [4

Get all possible combinations of elements

sa555 How would you get all possible combinations of 2 elements in an array? E.g: [ 1, 2, 3, 4 ] becomes [ [1, 2], [1, 3], [1, 4], [2, 1], [2, 3], [2, 4], [3, 1], [3, 2], [3, 4], [4, 1], [4

Get all possible combinations of elements

sa555 How would you get all possible combinations of 2 elements in an array? E.g: [ 1, 2, 3, 4 ] becomes [ [1, 2], [1, 3], [1, 4], [2, 1], [2, 3], [2, 4], [3, 1], [3, 2], [3, 4], [4, 1], [4

Get all possible combinations of elements

sa555 How would you get all possible combinations of 2 elements in an array? E.g: [ 1, 2, 3, 4 ] becomes [ [1, 2], [1, 3], [1, 4], [2, 1], [2, 3], [2, 4], [3, 1], [3, 2], [3, 4], [4, 1], [4

Get all possible combinations of elements

sa555 How would you get all possible combinations of 2 elements in an array? E.g: [ 1, 2, 3, 4 ] becomes [ [1, 2], [1, 3], [1, 4], [2, 1], [2, 3], [2, 4], [3, 1], [3, 2], [3, 4], [4, 1], [4

Get all possible combinations of elements

sa555 How would you get all possible combinations of 2 elements in an array? E.g: [ 1, 2, 3, 4 ] becomes [ [1, 2], [1, 3], [1, 4], [2, 1], [2, 3], [2, 4], [3, 1], [3, 2], [3, 4], [4, 1], [4

Get all possible combinations of elements

sa555 How would you get all possible combinations of 2 elements in an array? E.g: [ 1, 2, 3, 4 ] becomes [ [1, 2], [1, 3], [1, 4], [2, 1], [2, 3], [2, 4], [3, 1], [3, 2], [3, 4], [4, 1], [4

Get all possible combinations of elements

sa555 How would you get all possible combinations of 2 elements in an array? E.g: [ 1, 2, 3, 4 ] becomes [ [1, 2], [1, 3], [1, 4], [2, 1], [2, 3], [2, 4], [3, 1], [3, 2], [3, 4], [4, 1], [4

Get all possible combinations of elements

sa555 How would you get all possible combinations of 2 elements in an array? E.g: [ 1, 2, 3, 4 ] becomes [ [1, 2], [1, 3], [1, 4], [2, 1], [2, 3], [2, 4], [3, 1], [3, 2], [3, 4], [4, 1], [4

Get all possible combinations of elements

sa555 How would you get all possible combinations of 2 elements in an array? E.g: [ 1, 2, 3, 4 ] becomes [ [1, 2], [1, 3], [1, 4], [2, 1], [2, 3], [2, 4], [3, 1], [3, 2], [3, 4], [4, 1], [4

Python - How to get all possible combinations from a nested list

Hamza Arshad: I have nested lists like this: values = [['DNO', 0.2], ['Equinor', 0.4], ['Petoro', 0.2], ['Total', 0.2]] How can I get all possible combinations of elements that will sum (the second element of each sublist) greater than 0.5? This is what I'm u

Python - How to get all possible combinations from a nested list

Hamza Arshad: I have nested lists like this: values = [['DNO', 0.2], ['Equinor', 0.4], ['Petoro', 0.2], ['Total', 0.2]] How can I get all possible combinations of elements that will sum (the second element of each sublist) greater than 0.5? This is what I'm u

Python - How to get all possible combinations from a nested list

Hamza Arshad: I have nested lists like this: values = [['DNO', 0.2], ['Equinor', 0.4], ['Petoro', 0.2], ['Total', 0.2]] How can I get all possible combinations of elements that will sum (the second element of each sublist) greater than 0.5? This is what I'm u

intersect with all possible combinations of list elements

Enriferorello I have a list of vectors: > l <- list(A=c("one", "two", "three", "four"), B=c("one", "two"), C=c("two", "four", "five", "six"), D=c("six", "seven")) > l $A [1] "one" "two" "three" "four" $B [1] "one" "two" $C [1] "two" "four" "five" "six"

Generate a list of all possible combinations of vector elements

You Ma I am trying to generate all possible combinations of 0s and 1s in a vector of length 14. Is there an easy way to get the output as a list of vectors or better a dataframe? To better demonstrate what I'm looking for, let's assume I just want a vector of

intersect with all possible combinations of list elements

Enriferorello I have a list of vectors: > l <- list(A=c("one", "two", "three", "four"), B=c("one", "two"), C=c("two", "four", "five", "six"), D=c("six", "seven")) > l $A [1] "one" "two" "three" "four" $B [1] "one" "two" $C [1] "two" "four" "five" "six"

intersect with all possible combinations of list elements

Enriferorello I have a list of vectors: > l <- list(A=c("one", "two", "three", "four"), B=c("one", "two"), C=c("two", "four", "five", "six"), D=c("six", "seven")) > l $A [1] "one" "two" "three" "four" $B [1] "one" "two" $C [1] "two" "four" "five" "six"