找到最不受欢迎的爱好



此任务要求我编写一个函数,以列表的形式返回最不受欢迎的爱好。

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

例如,

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

应仅返回CCD_ 1。我设法实现了以下目标:

[('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)]

这(当然如果可能的话(如何转化为['dance', 'flowers', 'puzzles', 'tennis']

您可以使用python的sort函数,其中包含一个自定义密钥:

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

为了从这个元组列表中获得您感兴趣的字符串列表,您可以使用zip(*l),例如:

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

(b将包含相应爱好的有序频率(

最新更新