Home > database >  Find the least popular hobbies
Find the least popular hobbies

Time:10-14

This task requires me to write a function that returns the most unpopular hobbies alhebetically sorted in the form of a list.

def find_least_popular_hobbies(data: str) -> list:
    new_dict = create_dictionary(data) # creates a dictionary where the key is the name and the value is the hobby
    sorted_dict = sort_dictionary(new_dict) # sorts the dictionary values alphabetically
    new_list = []
    for value in sorted_dict.items():
        for ele in value[1]:
            new_list.append(ele)
    count = [(x, new_list.count(x)) for x in new_list]
    return count

For example,

print("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")

should only return ['dance', 'flowers', 'puzzles', 'tennis']. I managed to achieve the following:

[('baking', 4), ('crafting', 3), ('driving', 5), ('gaming', 6), ('shopping', 3), ('sport', 6), ('travel', 2), ('cooking', 4), ('dance', 1), ('driving', 5), ('football', 6), ('movies', 2), ('pets', 4), ('photography', 4), ('shopping', 3), ('sport', 6), ('theatre', 5), ('driving', 5), ('football', 6), ('gaming', 6), ('hiking', 5), ('origami', 3), ('pets', 4), ('sport', 6), ('yoga', 2), ('cooking', 4), ('fitness', 3), ('football', 6), ('gaming', 6), ('hiking', 5), ('movies', 2), ('yoga', 2), ('baking', 4), ('cooking', 4), ('crafting', 3), ('drawing', 2), ('fitness', 3), ('football', 6), ('gaming', 6), ('hiking', 5), ('origami', 3), ('pets', 4), ('skateboarding', 3), ('sport', 6), ('design', 3), ('fishing', 3), ('hiking', 5), ('skateboarding', 3), ('theatre', 5), ('baking', 4), ('design', 3), ('flowers', 1), ('pets', 4), ('photography', 4), ('skateboarding', 3), ('tennis', 1), ('theatre', 5), ('cooking', 4), ('drawing', 2), ('driving', 5), ('fishing', 3), ('gaming', 6), ('hiking', 5), ('origami', 3), ('photography', 4), ('theatre', 5), ('baking', 4), ('crafting', 3), ('design', 3), ('driving', 5), ('football', 6), ('sport', 6), ('travel', 2), ('fishing', 3), ('fitness', 3), ('football', 6), ('gaming', 6), ('photography', 4), ('puzzles', 1), ('shopping', 3), ('sport', 6), ('theatre', 5)]

How can this (if possible of course) be transformed into ['dance', 'flowers', 'puzzles', 'tennis']?

CodePudding user response:

You can use the sort function of python, with a custom key:

>>> l = [('baking', 4), ('crafting', 3), ('driving', 5)]
>>> l.sort(key=lambda x: x[1], reverse=True)
>>> l
[('driving', 5), ('baking', 4), ('crafting', 3)]

and to get from this list of tuple to a list of the strings you are interested in, you can use zip(*l) for instance:

>>> a, b = zip(*l)
>>> list(a)
['driving', 'baking', 'crafting']

(b will contain the ordered frequencies for the corresponding hobbies)

  • Related