按流派过滤电影-Python电影数据库

时间:2018-08-28 23:26:15

标签: python python-3.x

我正在尝试使用python的内置filter()函数来使用指定体裁过滤掉电影标题。有指针吗?另一个选择是使用一个空列表,然后将已过滤的电影添加到该列表中,但我也无法正常工作。

class Movie():
    def __init__(self, title, year, director, genre, language, actors, summary):
        self.title = title
        self.year = year
        self.director = director
        self.genre = genre
        self.language = language
        self.actors = actors
        self.summary = summary
        return
    def __str__(self):
        string = """
        Title: {}
        Year: {}
        Director: {}
        Genre: {}
        Language: {}
        Actors: {}
        Summary: {}""".format(self.title, self.year, self.director, self.genre, self.language, self.actors, self.summary)
        return string
# this is the filter function
def by_genre(movie_list, genre):
    movies = filter(movie_list, genre)
    return movies
#everything to this point is functioning properly    
# these are movies added to the database
Rampage = Movie('Rampage', 2018, 'Brad Peyton', 'scifi', 'English', 'The Rock', ' The Rock must rescue a endangered gorilla from poachers')
G_Galaxy = Movie('Guardians of the Galaxy', 2017, 'James Gunn', 'scifi', 'English', 'Chris Pratt', 'Chris Pratt and his ragtag group must save the universe')
Avatar = Movie('Avatar', 2009, 'James Cameron', 'scifi', 'English', 'Zoe Saldana', 'a paralyzed former Marine becomes an Avatar and falls in love, \
he is drawn into a battle for the survival of her world')
Jump = Movie('22 Jump Street', 2014, 'Phil Lord, Chris Miller', 'action', 'English', 'Channing Tatum, Jonah Hill', 'Undercover agents disguised as college students must crack a case')
Dpool2 = Movie('Deadpool 2', 2018, 'David Leitch', 'Fantasy', 'English', 'Ryan Reynolds', 'Deadpool must save a teenager from a time traveler')

#these are test codes
scifi_list = by_genre([Rampage, Avatar, Jump], 'scifi')
for m in scifi_list: print(m.title)

1 个答案:

答案 0 :(得分:1)

您的过滤器函数需要具有一个在项目通过标准时返回True的函数。查看以下内容:

def by_genre(movies_list, genre):
    return filter(lambda m: m.genre == genre,
                  movies_list)