0

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.

4
  • Can you explain a bit further? Possibly giving a good example of what you expect to see and maybe data we can work with like csv etc. Can you provide some information on the data and what each column is. Thanks. Commented Feb 23, 2022 at 20:00
  • it is just randomized data and it has no specific column headers, the output that is expected has been already outlined in the question Commented Feb 23, 2022 at 20:09
  • Do you want multiple rows for "Tommy" or only that one row? When you have Sally who has the same three sports, does the Row(s) for Sally get a 1 or a 2. Same question for Tommy. Basically, we need additional example input data and concreate full example output data, otherwise people are just guessing at how to help. Commented Feb 23, 2022 at 20:20
  • I want for Tommy only that one row: "Tommy = Football + Rugby + Baseball". For Sally and Tommy, both would get a 1 as they share the same three sports. Commented Feb 23, 2022 at 21:19

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.