Home > database >  How do I pair up multiple keys with one values from a dictionary through a function?
How do I pair up multiple keys with one values from a dictionary through a function?

Time:10-13

I have the following problem and I cannot figure out what to do. Can someone help???

"You are throwing a party and want to pair up people with similar interests. You have already collected data about each of your guest’s favorite interest in a dictionary like so:

interests = { 'Sophia' : 'Computer', 'Anders' : 'Dancing', 'Mette' : 'Dancing', 'Hikari' : 'Computer', 'Riko' : 'Rowing', 'Troels' : 'Rowing', 'Sarah' : 'Rowing' }

To be able to quickly suggest topics at the party, you need to convert this dictionary into a different layout.

Your task is to write a function transform_dictionary that takes as argument a dictionary such as the one above and returns a dictionary in which each key is an interest and the value is a list of all the people that share this interest. For example, for the list above, the dictionary that is returned is (disregarding a particular order of elements)

I need to print:

{'Computer': ['Sophia', 'Hikari'], 'Dancing': ['Anders', 'Mette'], 'Rowing': ['Riko', 'Troels', 'Sarah']}

This is what I have so far:

    interests = {
        'Sophia' : 'Computer',
        'Anders' : 'Dancing',
        'Mette' : 'Dancing', 
        'Hikari' : 'Computer',
        'Riko' : 'Rowing', 
        'Troels' : 'Rowing',
        'Sarah' : 'Rowing'
    }
    
    # comvert dictionary into a different layout.
    
    def transform_dictionary(interests):
        new_dict = {}
        key_list = []
        
        for k, v in new_dict:
            if value in new_dict:
            new_dict[value] = k
        return {}
        
    
    print(transform_dictionary(interests)) 

CodePudding user response:

You can use collections.defaultdict(list) then only use append like below:

from collections import defaultdict
dct = defaultdict(list)

for k,v in interests.items():
    dct[v].append(k)

Output:

{'Computer': ['Sophia', 'Hikari'],
 'Dancing': ['Anders', 'Mette'],
 'Rowing': ['Riko', 'Troels', 'Sarah']}

CodePudding user response:

You can do it like that:

interests = {
    'Sophia' : 'Computer',
    'Anders' : 'Dancing',
    'Mette' : 'Dancing', 
    'Hikari' : 'Computer',
    'Riko' : 'Rowing', 
    'Troels' : 'Rowing',
    'Sarah' : 'Rowing'
}

# convert dictionary into a different layout.

def transform_dictionary(interests):
    new_dict = {}
    for v in interests.values():
        new_dict[v] = [i for i in interests if interests[i] == v]
    return new_dict
    

print(transform_dictionary(interests))

Output:

{'Computer': ['Sophia', 'Hikari'], 'Dancing': ['Anders', 'Mette'], 'Rowing': ['Riko', 'Troels', 'Sarah']}

CodePudding user response:

This works:

def transform_dictionary(interests):
    new_dict = {}
    for key, value in interests.items():
        if not value in new_dict:
            new_dict[value] = [key]
        else:
            new_dict[value]  = [key]
    return new_dict
  • Related