从字典中的列表中删除非允许的对象

时间:2014-11-05 14:31:03

标签: python dictionary

我有一个字典,其中包含作为键的actor和作为值的电影标题列表。例如:

{'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

我想使用该功能删除原始dicstionary中电影标题列表中的每部电影。

我尝试过以下操作:

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:不可用的类型:' list'"

编辑: 流派词典可能是{' Heat' :'戏剧','时间是朦胧的' :'纪录片'},其中列表中所有与被列为非允许类型的演员相对应的电影片名应从原始字典中删除。

2 个答案:

答案 0 :(得分:1)

我试图理解哪个电影 Al自己想要忘记

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]: 

答案 1 :(得分:0)

您的示例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

使用any:

可以更简洁地编写代码
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