Home > Software design >  Python dictionary no duplicate names
Python dictionary no duplicate names

Time:10-21

def sort_dictionary(dict: dict) -> dict: 
    return {k: sorted(v) for k, v in dict.items()}

def create_dictionary_with_hobbies(data: str) -> dict:
    """Create dictionary about hobbies and their hobbyists ie. {hobby1: [name1, name2, ...], hobby2: [...]}."""
    result = {}
    for line in data.split('\n'):
        name, hobby = line.split(":")
        result.setdefault(hobby, []).append(name)
        result = sort_dictionary(result)
    return result 

if __name__ == '__main__':
    sample_data = """Jack:crafting\nPeter:hiking\nWendy:gaming\nMonica:tennis\nChris:origami\nSophie:sport\nMonica:design\nCarmen:sport\nChris:sport\nMonica:skateboarding\nCarmen:cooking\nWendy:photography\nMonica:tennis\nCooper:yoga\nWendy:sport\nCooper:movies\nMonica:theatre\nCooper:yoga\nChris:gaming\nMolly:fishing\nJack:skateboarding\nWendy:fishing\nJack:drawing\nMonica:baking\nSophie:baking\nAlfred:driving\nAlfred:shopping\nAlfred:crafting\nJack:drawing\nCarmen:shopping\nCarmen:driving\nPeter:drawing\nCarmen:shopping\nWendy:fitness\nAlfred:travel\nJack:origami\nSophie:design\nJack:pets\nCarmen:dance\nAlfred:baking\nSophie:sport\nPeter:gaming\nJack:skateboarding\nCooper:football\nAlfred:sport\nCooper:fitness\nChris:yoga\nWendy:football\nMolly:design\nJack:hiking\nMonica:pets\nCarmen:photography\nJack:baking\nPeter:driving\nChris:driving\nCarmen:driving\nPeter:theatre\nMolly:hiking\nWendy:puzzles\nJack:crafting\nPeter:photography\nCarmen:theatre\nSophie:crafting\nCarmen:cooking\nAlfred:gaming\nPeter:theatre\nCooper:hiking\nChris:football\nChris:pets\nJack:football\nMonica:skateboarding\nChris:driving\nCarmen:pets\nCooper:gaming\nChris:hiking\nJack:cooking\nPeter:fishing\nJack:gaming\nPeter:origami\nCarmen:movies\nSophie:driving\nJack:sport\nCarmen:theatre\nWendy:shopping\nCarmen:pets\nWendy:gaming\nSophie:football\nWendy:theatre\nCarmen:football\nMolly:theatre\nPeter:theatre\nMonica:flowers\nMolly:skateboarding\nPeter:driving\nSophie:travel\nMonica:photography\nCooper:cooking\nJack:fitness\nPeter:cooking\nChris:gaming"""
    print(create_dictionary_with_hobbies(sample_data))

Result:

{'crafting': ['Alfred', 'Jack', 'Jack', 'Sophie'], 'hiking': ['Chris', 'Cooper', 'Jack', 'Molly', 'Peter'], 'gaming': ['Alfred', 'Chris', 'Chris', 'Cooper', 'Jack', 'Peter', 'Wendy', 'Wendy'], 'tennis': ['Monica', 'Monica'], 'origami': ['Chris', 'Jack', 'Peter'], 'sport': ['Alfred', 'Carmen', 'Chris', 'Jack', 'Sophie', 'Sophie', 'Wendy'], 'design': ['Molly', 'Monica', 'Sophie'], 'skateboarding': ['Jack', 'Jack', 'Molly', 'Monica', 'Monica'], 'cooking': ['Carmen', 'Carmen', 'Cooper', 'Jack', 'Peter'], 'photography': ['Carmen', 'Monica', 'Peter', 'Wendy'], 'yoga': ['Chris', 'Cooper', 'Cooper'], 'movies': ['Carmen', 'Cooper'], 'theatre': ['Carmen', 'Carmen', 'Molly', 'Monica', 'Peter', 'Peter', 'Peter', 'Wendy'], 'fishing': ['Molly', 'Peter', 'Wendy'], 'drawing': ['Jack', 'Jack', 'Peter'], 'baking': ['Alfred', 'Jack', 'Monica', 'Sophie'], 'driving': ['Alfred', 'Carmen', 'Carmen', 'Chris', 'Chris', 'Peter', 'Peter', 'Sophie'], 'shopping': ['Alfred', 'Carmen', 'Carmen', 'Wendy'], 'fitness': ['Cooper', 'Jack', 'Wendy'], 'travel': ['Alfred', 'Sophie'], 'pets': ['Carmen', 'Carmen', 'Chris', 'Jack', 'Monica'], 'dance': ['Carmen'], 'football': ['Carmen', 'Chris', 'Cooper', 'Jack', 'Sophie', 'Wendy'], 'puzzles': ['Wendy'], 'flowers': ['Monica']}

Maybe you could suggest what function can add to remove duplicates. Let's say in the first crafting answer there are two Jacks, but there should be one. But the alphabetical order of names should also be preserved, like it now is. I understand that no one will write the code for me, but it would be nice to get an answer to this question.

CodePudding user response:

You can just change result.setdefault(hobby, []).append(name)

to result.setdefault(hobby, set()).add(name)

Set will be sorted by sorted() function.

CodePudding user response:

Adding the items to a dict with set values will remove duplicates. You can then convert the sets to lists and sort them.

hobbies_dict = {}
for item in sample_data.split(): 
    v, k = item.split(':')
    if k not in hobbies_dict:
        hobbies_dict[k] = set([v])
    else:
        hobbies_dict[k].add(v)

for k,v in hobbies_dict.items(): # convert set to list and order items
    hobbies_dict[k] = sorted(list(v))

CodePudding user response:

For Python 3.7 or later you can use the feature that dictionaries maintain order (and that duplicate keys are not allowed). Which means you can simply use:

for k, v in mydict.items():
    mydict[k] = list(dict.fromkeys(v))

which will remove duplicates from the lists in the dictionary

  • Related