Getting All Possible Combinations from a List With Duplicate Elements

Getting all possible combinations from a list with duplicate elements?

You can take the set of the combinations and then chain 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)]

Find all combinations of list elements including duplicate elements

here is a brute force solution:

def getAllSubsetsWithCertainSum(number_list, target_sum):

matching_numbers = []

def recursion(subset):
for number in number_list:
if sum(subset+[number]) < target_sum:
recursion(subset+[number])
elif sum(subset+[number]) == target_sum:
matching_numbers.append(subset+[number])

recursion([])
return matching_numbers


print(
getAllSubsetsWithCertainSum([2, 3, 4], 7)
)

If you input a 1 it will also return [1, 1, 1, 1, 1, 1, 1]

How to find all possible combinations from a list with only two elements and no duplicates with Python?

You can try this, as it makes use of combinations so import it from itertools like this:

from itertools import combinations

#also it's good practice not to use list, so for sake of it call it something else
listPlayers = ["player1", "player2", "player3"]

getPairPlayers = sorted(map(sorted, combinations(set(listPlayers), 2)))

print(getPairPlayers)

Output:

[['player1', 'player2'], ['player1', 'player3'], ['player2', 'player3']]

generating list of every combination without duplicates

There will certainly be more sophisticated/efficient ways of doing this, but here's an approach that works in a reasonable amount of time for your example and should be easy enough to adapt for other cases.

It generates unique teams and unique combinations thereof, as per your specifications.

from itertools import combinations

# this assumes that team_size * team_num == len(players) is a given
team_size = 3
team_num = 4
players = list('ABCDEFGHIJKL')
unique_teams = [set(c) for c in combinations(players, team_size)]

def duplicate_player(combo):
"""Returns True if a player occurs in more than one team"""
return len(set.union(*combo)) < len(players)

result = (combo for combo in combinations(unique_teams, team_num) if not duplicate_player(combo))

result is a generator that can be iterated or turned into a list with list(result). On kaggle.com, it takes a minute or so to generate the whole list of all possible combinations (a total of 15400, in line with the computations by @beaker and @John Coleman in the comments). The teams are tuples of sets that look like this:

[({'A', 'B', 'C'}, {'D', 'E', 'F'}, {'G', 'H', 'I'}, {'J', 'K', 'L'}),
({'A', 'B', 'C'}, {'D', 'E', 'F'}, {'G', 'H', 'J'}, {'I', 'K', 'L'}),
({'A', 'B', 'C'}, {'D', 'E', 'F'}, {'G', 'H', 'K'}, {'I', 'J', 'L'}),
...
]

If you want, you can cast them into strings by calling ''.join() on each of them.

How to generate all distinct combinations (where input elements are repeated) in Python(using Itertools)?

These are called multisets, and we can easily obtain the combinations of these with the sympy module.

from sympy.utilities.iterables import multiset_combinations

list(multiset_combinations([1, 2, 4, 1], 3))
[[1, 1, 2], [1, 1, 4], [1, 2, 4]]

And here is the example by @EdedkiOkoh:

x = [-1, 0, 1, 2, -1, -4]
list(multiset_combinations(x, 3))
[[-4, -1, -1],
[-4, -1, 0],
[-4, -1, 1],
[-4, -1, 2],
[-4, 0, 1],
[-4, 0, 2],
[-4, 1, 2],
[-1, -1, 0],
[-1, -1, 1],
[-1, -1, 2],
[-1, 0, 1],
[-1, 0, 2],
[-1, 1, 2],
[0, 1, 2]]

Unique combinations from a set with duplicates

from more_itertools import distinct_combinations

print(*distinct_combinations([2, 2, 3], 2))

Output:

(2, 2) (2, 3)

Documentation, which says:

Equivalent to set(combinations(iterable)), except duplicates are not generated and thrown away. For larger input sequences this is much more efficient.

Print all possible combination of pairs of element's list

Take a look at combinations in itertools.

>>> l = [(1,2), (1,3), (1,4), (2,3), (2, 4), (3,4)]
>>> import itertools
>>> combinations = itertools.combinations(l, 2)
>>> for combination in combinations:
... print(combination)
...
((1, 2), (1, 3))
((1, 2), (1, 4))
((1, 2), (2, 3))
((1, 2), (2, 4))
((1, 2), (3, 4))
((1, 3), (1, 4))
((1, 3), (2, 3))
((1, 3), (2, 4))
((1, 3), (3, 4))
((1, 4), (2, 3))
((1, 4), (2, 4))
((1, 4), (3, 4))
((2, 3), (2, 4))
((2, 3), (3, 4))
((2, 4), (3, 4))


Related Topics



Leave a reply



Submit