簡體   English   中英

從字典中的列表中刪除不允許的對象

[英]Removing non-allowed objects from a list in a dictionary

我有一本字典,其中包含演員作為鍵,而電影標題列表作為值。 例:

{'Pacino, Al' : ['Scarface', 'Godfather', 'Heat', ...]}

然后,我有一個函數,該函數將帶有電影標題的字典作為鍵,將流派作為值作為參數:

def is_movie(genre_dict, title):
    disallowed_genres = ["Reality-TV", "News", "Talk-Show",
                     "Game-Show", "Lifestyle", "Commercial",
                     "Documentary", "Music", "Biography"]
    if title in genre_dict and not "(TV)" in title:
        any_disallowed = False
        for genre in genre_dict[title]:
            any_disallowed = (any_disallowed or (genre in disallowed_genres))
        return not any_disallowed
    else:
        return False

我想使用該功能刪除原始詞典中電影標題列表中的每部電影。

我嘗試執行以下操作:

def filter_actor_dictionary(actor_dict, genre_dict):
    temp_dict=actor_dict.copy() #Creates a copy of actor_dict
    for item in actor_dict.iteritems():
        if not is_movie(genre_dict, item):
           del temp_dict[item]
    return temp_dict

這給我“ TypeError:無法散列的類型:'列表'”

編輯:流派字典可以是{'Heat':'Drama','Time is Illmatic':'Documentary'},其中應刪除列表中與該演員相對應的所有電影標題(被列為不允許的流派)從我原來的字典里。

我試圖了解艾爾本人想忘記哪部電影

In [1]: %colors LightBg

In [2]: d = {'Pacino, Al' : ['Scarface', 'Godfather', 'Heat',]}

In [3]: g = {'Scarface':1, 'Godfather':2, 'Heat':3}

In [4]: bad_g = [3,]

In [5]: def no_bad_genres(d,g,bad_g):                       
    for actor in d.keys():
        films = d[actor]
        for n, film in enumerate(films):
            if g[film] in bad_g:
                del films[n]
   ...:                 

In [6]: no_bad_genres(d,g,bad_g) ;  print d
{'Pacino, Al': ['Scarface', 'Godfather']}

In [7]: 

在您的示例item將是[('Pacino, Al', ['Scarface', 'Godfather', 'Heat'])]因此temp_dict[item]嘗試使用該列表作為鍵來刪除,如下所示:

temp_dict[[('Pacino, Al', ['Scarface', 'Godfather', 'Heat'])]] 

使用del temp_dict[item[0]]訪問密鑰,或僅在actor_dict上進行迭代以僅訪問密鑰並使用temp_dict[item]

根據需要使用is_movie

def filter_actor_dictionary(actor_dict ,genre_dict):
    temp_dict = actor_dict.copy()  #Creates a copy of actor_dict
    for key, val in actor_dict.iteritems():
        for title in val:
            if is_movie(genre_dict,title):
                temp_dict[key].remove(title)
    return temp_dict

is_moviegenre_dict某些部分不正確,應該更像以下內容:

# need to store in a list or for genre in genre_dict[title] will be iterating over individual characters
genre_dict = {'Heat': ['Drama',"Talk-Show"], 'Time is Illmatic': ['Documentary']}

def is_movie(genre_dict, title):
    disallowed_genres = ["Reality-TV", "Drama","News", "Talk-Show",
                     "Game-Show", "Lifestyle", "Commercial",
                     "Documentary", "Music", "Biography"]
    if title in genre_dict and not "(TV)" in title:
        for genre in genre_dict[title]:
            # should return True if any genre is in disallowed_genres
            if genre in disallowed_genres:
                return True
    return False

可以使用以下任何方式更簡潔地編寫代碼:

def is_movie(genre_dict, title):
    disallowed_genres = ["Reality-TV", "Drama","News", "Talk-Show",
                     "Game-Show", "Lifestyle", "Commercial",
                     "Documentary", "Music", "Biography"]
    if title in genre_dict and not "(TV)" in title:
            return any(genre in disallowed_genres for genre in genre_dict[title])
    return False

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM