Reputation:
let's assume I have an excel table of this structure
Max 27 2 Tennis
Tommy 29 1 Football
Tommy 20 1 Rugby
Tommy 8 9 Baseball
How can I calculate all possible combinations for column 4 based on the first column? For example the output should be looking like this:
Max = Tennis
Tommy = Football + Rugby + Baseball
Is there a possible way to do that programmatically in python? If so, how can I count the number of the repeated combination?
For example the output may be:
Football + Rugby + Baseball: 1
So this combination is repeated only once.
Upvotes: 0
Views: 386
Reputation: 168996
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.
Upvotes: 1