I have the following data structure - list of dictionaries with movies, bucketized by genre (it's very large, here just a sample):
data={'Action': [{'title': 'They Live',
'year': 1988,
'genres': ['Action', 'Horror', 'Sci-Fi'],
'duration': 94,
'directors': ['John Carpenter'],
'actors': ['Roddy Piper', 'Keith David', 'Meg Foster'],
'rating': 7.3},
{'title': 'Ultra Warrior',
'year': 1990,
'genres': ['Action', 'Adventure', 'Sci-Fi'],
'duration': 81,
'directors': ['Augusto Tamayo San Román', 'Kevin Tent'],
'actors': ['Dack Rambo',
'Clare Beresford',
'Meshach Taylor',
'Mark Bringelson'],
'rating': 1.9},
{'title': 'Kick-As 2',
'year': 2013,
'genres': ['Action', 'Comedy', 'Crime'],
'duration': 103,
'directors': ['Jeff Wadlow'],
'actors': ['Aaron Taylor-Johnson', 'Chloë Grace Moretz'],
'rating': 6.5}],
'Drama': [{'title': 'Dirty Beautiful',
'year': 2015,
'genres': ['Comedy', 'Drama', 'Romance'],
'duration': 95,
'directors': ['Jodie Foster'],
'actors': ['Jodie Foster', 'Ricky Mabe', 'Jordan Monaghan', 'Conor Leslie', 'Darin Heames'],
'rating': 5.5},
{'title': 'Honeydripper',
'year': 2007,
'genres': ['Crime', 'Drama', 'History'],
'duration': 124,
'directors': ['John Sayles'],
'actors': ['Danny Glover', 'LisaGay Hamilton', 'Yaya DaCosta'],
'rating': 6.6}]}
I'm trying to pull a list of all movies in which 'Jodie Foster' is both an actor and director. As well as find the title of the longest movie both acted and directed by 'Clint Eastwood'?
Here's what I tried so far, but I'm a bit stuck, cause the returned list is empty... and it really shouldnt.
for i in data:
d=data[i][0]
if 'Jodie Foster' in d['directors'] and 'Jodie Foster' in d['actors']:
movies.append(d["title"])
movies
movie="None"
maximum=0
for i in data:
d=data[i][0]
if 'Clint Eastwood' in d['directors'] and 'Clint Eastwood' in d['actors']:
if d["duration"]>maximum:
maximum=d["duration"]
movie=d["title"]
Would greatly appreciate any help...
CodePudding user response:
You have a strangely structured dictionary in my opinion, but that's just a preference thing. Here's working code for what you want:
import json
print(json.dumps(data, indent=4)) # <--- cleanly printed JSON-like data
def movies_of_actor(actor_name):
movies = []
for genre, genre_list in data.items():
for movie_info in genre_list:
if actor_name in movie_info['actors']:
movies.append(movie_info['title'])
return movies
def movies_of_director(director_name):
movies = []
for genre, genre_list in data.items():
for movie_info in genre_list:
if director_name in movie_info['directors']:
movies.append(movie_info['title'])
return movies
def longest_title_of_actor(actor_name):
longest_title = None
length = 0
for genre, genre_list in data.items():
for movie_info in genre_list:
if actor_name in movie_info['actors']:
title = movie_info['title']
if length < len(title):
longest_title = title
return longest_title
def longest_title_of_director(actor_name):
longest_title = None
length = 0
for genre, genre_list in data.items():
for movie_info in genre_list:
if actor_name in movie_info['directors']:
title = movie_info['title']
if length < len(title):
longest_title = title
return longest_title
m1 = movies_of_actor('Jodie Foster')
m2 = movies_of_director('Jodie Foster')
# make sure the movie is in both
mt = []
for m in m1:
if m in m2:
mt.append(m)
print('Movies that Jodie Foster acted in and directed:', mt)
m3 = longest_title_of_actor('Clint Eastwood')
m4 = longest_title_of_director('Clint Eastwood')
print('Longest movie title that Clint Eastwood acted in:', m3)
print('Longest movie title that Clint Eastwood directed:', m4)
CodePudding user response:
# part 1
actor = 'Jodie Foster'
result = []
for films in data.values():
for film in films:
if actor in film['directors'] and actor in film['actors']:
result.append(film['title'])
if result:
print(result)
else:
print(f'No results for {actor}')
# part 2
actor = 'Clint Eastwood'
result_2 = []
for films in data.values():
for film in films:
if actor in film['directors'] and actor in film['actors']:
result_2.append(film['duration'])
if result_2:
print(max(result_2))
else:
print(f'No results for {actor}')