If you need permutations, sure thing.
import collections
import itertools
data = """
Max 27 2 Tennis
Tommy 29 1 Football
Tommy 20 1 Rugby
Tommy 8 9 Baseball
"""
# Generate mapping of person -> sports
grouped = collections.defaultdict[set]
for line in data.strip[].splitlines[]:
line = line.split[]
# line[0]: name
# line[-1]: sport
grouped[line[0]].add[line[-1]]
# Walk over the mapping and print out permutations per person.
for person, sports in grouped.items[]:
for sport_set in itertools.permutations[sports]:
print[person, sport_set]
This prints out
Max ['Tennis',]
Tommy ['Baseball', 'Rugby', 'Football']
Tommy ['Baseball', 'Football', 'Rugby']
Tommy ['Rugby', 'Baseball', 'Football']
Tommy ['Rugby', 'Football', 'Baseball']
Tommy ['Football', 'Baseball', 'Rugby']
Tommy ['Football', 'Rugby', 'Baseball']
itertools
also has functions for combinations
and combinations_with_replacement
, if those are what you're after.
Python provides direct methods to find permutations and combinations of a sequence. These methods are present in itertools package.
Permutation
First import itertools package to implement the permutations method in python. This method takes a list as an input and returns an object list of tuples that contain all permutations in a list form.
Python3
from
itertools
import
permutations
perm
=
permutations[[
1
,
2
,
3
]]
for
i
in
list
[perm]:
print
[i]
Output:
[1, 2, 3] [1, 3, 2] [2, 1, 3] [2, 3, 1] [3, 1, 2] [3, 2, 1]
It generates n! permutations if the length of the input sequence is n.
If want to get permutations of length L then implement it in this way.
Python3
from
itertools
import
permutations
perm
=
permutations[[
1
,
2
,
3
],
2
]
for
i
in
list
[perm]:
print
[i]
Output:
[1, 2] [1, 3] [2, 1] [2, 3] [3, 1] [3, 2]
It generates nCr * r! permutations if the length of the input sequence is n and the input parameter is r.
Combination
This method takes a list and an input r as an input and return an object list of tuples which contain all possible combination of length r in a list form.
Python3
from
itertools
import
combinations
comb
=
combinations[[
1
,
2
,
3
],
2
]
for
i
in
list
[comb]:
print
[i]
Output:
[1, 2] [1, 3] [2, 3]
1. Combinations are emitted in lexicographic sort order of input. So, if the input list is sorted, the combination tuples will be produced in sorted order.
Python3
from
itertools
import
combinations
comb
=
combinations[[
1
,
2
,
3
],
2
]
for
i
in
list
[comb]:
print
[i]
Output:
[1, 2] [1, 3] [2, 3]
2. Elements are treated as unique based on their position, not on their value. So if the input elements are unique, there will be no repeat values in each combination.
Python3
from
itertools
import
combinations
comb
=
combinations[[
2
,
1
,
3
],
2
]
for
i
in
list
[comb]:
print
[i]
Output:
[2, 1] [2, 3] [1, 3]
3. If we want to make a combination of the same element to the same element then we use combinations_with_replacement.
Python3
from
itertools
import
combinations_with_replacement
comb
=
combinations_with_replacement[[
1
,
2
,
3
],
2
]
for
i
in
list
[comb]:
print
[i]
Output:
[1, 1] [1, 2] [1, 3] [2, 2] [2, 3] [3, 3]