Find the least popular hobbies
Question:
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:craftingnPeter:hikingnWendy:gamingnMonica:tennisnChris:origaminSophie:sportnMonica:designnCarmen:sportnChris:sportnMonica:skateboardingnCarmen:cookingnWendy:photographynMonica:tennisnCooper:yoganWendy:sportnCooper:moviesnMonica:theatrenCooper:yoganChris:gamingnMolly:fishingnJack:skateboardingnWendy:fishingnJack:drawingnMonica:bakingnSophie:bakingnAlfred:drivingnAlfred:shoppingnAlfred:craftingnJack:drawingnCarmen:shoppingnCarmen:drivingnPeter:drawingnCarmen:shoppingnWendy:fitnessnAlfred:travelnJack:origaminSophie:designnJack:petsnCarmen:dancenAlfred:bakingnSophie:sportnPeter:gamingnJack:skateboardingnCooper:footballnAlfred:sportnCooper:fitnessnChris:yoganWendy:footballnMolly:designnJack:hikingnMonica:petsnCarmen:photographynJack:bakingnPeter:drivingnChris:drivingnCarmen:drivingnPeter:theatrenMolly:hikingnWendy:puzzlesnJack:craftingnPeter:photographynCarmen:theatrenSophie:craftingnCarmen:cookingnAlfred:gamingnPeter:theatrenCooper:hikingnChris:footballnChris:petsnJack:footballnMonica:skateboardingnChris:drivingnCarmen:petsnCooper:gamingnChris:hikingnJack:cookingnPeter:fishingnJack:gamingnPeter:origaminCarmen:moviesnSophie:drivingnJack:sportnCarmen:theatrenWendy:shoppingnCarmen:petsnWendy:gamingnSophie:footballnWendy:theatrenCarmen:footballnMolly:theatrenPeter:theatrenMonica:flowersnMolly:skateboardingnPeter:drivingnSophie:travelnMonica:photographynCooper:cookingnJack:fitnessnPeter:cookingnChris: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']
?
Answers:
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)
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:craftingnPeter:hikingnWendy:gamingnMonica:tennisnChris:origaminSophie:sportnMonica:designnCarmen:sportnChris:sportnMonica:skateboardingnCarmen:cookingnWendy:photographynMonica:tennisnCooper:yoganWendy:sportnCooper:moviesnMonica:theatrenCooper:yoganChris:gamingnMolly:fishingnJack:skateboardingnWendy:fishingnJack:drawingnMonica:bakingnSophie:bakingnAlfred:drivingnAlfred:shoppingnAlfred:craftingnJack:drawingnCarmen:shoppingnCarmen:drivingnPeter:drawingnCarmen:shoppingnWendy:fitnessnAlfred:travelnJack:origaminSophie:designnJack:petsnCarmen:dancenAlfred:bakingnSophie:sportnPeter:gamingnJack:skateboardingnCooper:footballnAlfred:sportnCooper:fitnessnChris:yoganWendy:footballnMolly:designnJack:hikingnMonica:petsnCarmen:photographynJack:bakingnPeter:drivingnChris:drivingnCarmen:drivingnPeter:theatrenMolly:hikingnWendy:puzzlesnJack:craftingnPeter:photographynCarmen:theatrenSophie:craftingnCarmen:cookingnAlfred:gamingnPeter:theatrenCooper:hikingnChris:footballnChris:petsnJack:footballnMonica:skateboardingnChris:drivingnCarmen:petsnCooper:gamingnChris:hikingnJack:cookingnPeter:fishingnJack:gamingnPeter:origaminCarmen:moviesnSophie:drivingnJack:sportnCarmen:theatrenWendy:shoppingnCarmen:petsnWendy:gamingnSophie:footballnWendy:theatrenCarmen:footballnMolly:theatrenPeter:theatrenMonica:flowersnMolly:skateboardingnPeter:drivingnSophie:travelnMonica:photographynCooper:cookingnJack:fitnessnPeter:cookingnChris: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']
?
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)