【问题标题】:How to sort dictionaries and create a dictionaries based on the values in the list inside the dictionary如何根据字典内列表中的值对字典进行排序并创建字典
【发布时间】:2021-04-10 18:34:17
【问题描述】:

这对我来说是一个挑战,我在学习过程中尝试了几个小时,但我不确定我的逻辑是否正确。

定义一个名为 stars 的函数,它接收两个字典:

movies:一个字典,其中的键是电影标题和 这些值是电影中主要表演者的名单。
例如:

movies["The Dark Knight"] = ["Christian Bale",
"Heath Ledger", "Maggie Gyllenhall", "Aaron Eckhart"]

电视节目:一个字典,其中的键是电视节目的标题 以及节目中主要表演者的价值观列表。 例如:

tvshows["Community"] = ["Joel McHale", "Alison
Brie", "Danny Pudi", "Donald Glover", "Yvette Brown"]

函数 stars 应该返回一个新字典。新字典的键应该是表演者的名字,每个键的值应该是节目列表和 该表演者出现的电影。对节目进行排序 和电影按字母顺序排列。

如果您的功能正常工作,这将最初打印(尽管键的顺序可能会有所不同):

{'Portia de Rossi': ['Arrested Development'], 'Will Ferrell': ['The Lego Movie'], 'Yvette Brown': ['Community'], 'Rebel Wilson': ['How to Be Single'], 'Danny Pudi': ['Community'], 'Elizabeth Banks': ['30 Rock', 'The Lego Movie'], 'Alec Baldwin': ['30 Rock'], 'Alison Brie': ['Community', 'How to Be Single', 'The Lego Movie'], 'Tina Fey': ['30 Rock'], 'Dakota Johnson': ['How to Be Single'], 'Joel McHale': ['Community'], 'Jack McBrayer': ['30 Rock'], 'Tracy Morgan': ['30 Rock'], 'Donald Glover': ['Community'], 'Will Arnett': ['Arrested Development', 'The Lego Movie'], 'Jason Bateman': ['Arrested Development']}

movies = {"How to Be Single": ["Alison Brie", "Dakota Johnson",
                               "Rebel Wilson"],
          "The Lego Movie": ["Will Arnett", "Elizabeth Banks",
                             "Alison Brie", "Will Ferrell"]}

tvshows = {"Community": ["Alison Brie", "Joel McHale",
                         "Danny Pudi", "Yvette Brown",
                         "Donald Glover"],
           "30 Rock": ["Tina Fey", "Tracy Morgan", "Jack McBrayer",
                       "Alec Baldwin", "Elizabeth Banks"],
           "Arrested Development": ["Jason Bateman", "Will Arnett",
                                    "Portia de Rossi"]}



print(stars(movies, tvshows))


def stars(movies, tv_shows):
    # print(movies)
    # print(tv_shows)
    dictionary_to_return = {}
    both_dict = {**movies, **tv_shows}
    # print(both_dict)
    celebrity_list = []
    # # print(celebrity_list)
    for (key, value) in both_dict.items():
        celebrity_list.extend(value)
        celebrity_list_filtered = list(set(celebrity_list))
    celebrity_list_filtered.sort()
    # print(celebrity_list_filtered)
    for every_celebrity in celebrity_list_filtered:
        # print(every_celebrity,": THis is artist")
        for every_title in both_dict.keys():
            # print(every_title,": THis is the tile")
            artist_in_title = both_dict[every_title]
            # print(artist_in_title, ":these are artist in the", every_title)
            if every_celebrity in artist_in_title:
                if every_title not in dictionary_to_return.keys():
                    celebrity_list.append(every_title)

                else:
                    valuess = dictionary_to_return[every_celebrity].value
                    print(valuess)
                print(celebrity_list)
    print(dictionary_to_return)
                # print(type(artist_in_title))
                # (every_celebrity,)
    # ret_list =[]
    # individual_tilte_list=[]
    # ret_dict ={}
    # for every_celebrity in celebrity_list_filtered:
    #     # print(every_celebrity)
    #     for (each_title,value) in both_dict.items():
    #         if every_celebrity in both_dict[each_title]:
    #             ret_list.append(every_celebrity)
    #             ret_list.append(key)
    # print(ret_list)
    # for i in range(0, len(ret_list)):
    #     if ret_list[i] in celebrity_list_filtered:
    #         if ret_list[i] in individual_tilte_list:
    #             individual_tilte_list.append(ret_list[0+i])
    #     elif ret_list[i] not in celebrity_list_filtered:
    #         individual_tilte_list.append(ret_list[i])
    # print(individual_tilte_list)

上面是我编写的代码,我觉得完全是垃圾。一些关于如何解决这个问题的反馈将不胜感激

【问题讨论】:

    标签: python-3.x list sorting dictionary


    【解决方案1】:

    我相信你需要。

    def stars(movies, tv_shows):
        dictionary_to_return = {}
        for k, v in {**movies, **tv_shows}.items():
            for actor in v:
                dictionary_to_return.setdefault(actor, []).append(k)
    
        # dictionary_to_return = {k: sorted(v) for k,v in dictionary_to_return.items()}  #Sort by show-movie
        return dictionary_to_return
    
    print(stars(movies, tvshows))
    

    输出:

    {'Alec Baldwin': ['30 Rock'],
     'Alison Brie': ['How to Be Single', 'The Lego Movie', 'Community'],
     'Dakota Johnson': ['How to Be Single'],
     'Danny Pudi': ['Community'],
     'Donald Glover': ['Community'],
     'Elizabeth Banks': ['The Lego Movie', '30 Rock'], 
     'Jack McBrayer': ['30 Rock'],
     'Jason Bateman': ['Arrested Development'],        
     'Joel McHale': ['Community'],
     'Portia de Rossi': ['Arrested Development'],      
     'Rebel Wilson': ['How to Be Single'],
     'Tina Fey': ['30 Rock'],
     'Tracy Morgan': ['30 Rock'],
     'Will Arnett': ['The Lego Movie', 'Arrested Development'],
     'Will Ferrell': ['The Lego Movie'],
     'Yvette Brown': ['Community']}
    

    【讨论】:

      猜你喜欢
      • 2022-11-14
      • 2018-09-27
      • 1970-01-01
      • 1970-01-01
      • 2021-05-29
      • 1970-01-01
      • 2011-02-22
      • 2021-12-29
      相关资源
      最近更新 更多