I am trying to create a list of all actors' co-actors. The exemplary input looks like this:
cast_info = [[
{'name': 'Drew Barrymore'},
{'name': 'Brian Herzlinger'},
{'name': 'Corey Feldman'},
{'name': 'Eric Roberts'}, {'name': 'Griffin Dunne'}, {'name': 'Samuel L. Jackson'}, {'name': 'Matt LeBlanc'}, {'name': "Bill D'Elia"}], [{'name': 'Erich Anderson'}, {'name': 'Judie Aronson'}, {'name': 'Peter Barton'}, {'name': 'Kimberly Beck'}, {'name': 'Tom Everett'}, {'name': 'Flashlight Man'}, {'name': 'Corey Feldman'}]]
and the expected output can have the following structure:
{'Drew Barrymore': ['Brian Herzlinger', 'Corey Feldman', 'Eric Roberts', 'Griffin Dunne', 'Samuel L. Jackson', 'Matt LeBlanc', "Bill D'Elia"],
'Brian Herzlinger': ['Drew Barrymore', 'Corey Feldman', 'Eric Roberts', 'Griffin Dunne', 'Samuel L. Jackson', 'Matt LeBlanc', "Bill D'Elia"],
'Corey Feldman': ['Drew Barrymore', 'Brian Herzlinger', 'Eric Roberts', 'Griffin Dunne', 'Samuel L. Jackson', 'Matt LeBlanc', "Bill D'Elia", 'Erich Anderson', 'Judie Aronson', 'Peter Barton', 'Kimberly Beck', 'Tom Everett', 'Flashlight Man'],
'Eric Roberts': ['Drew Barrymore', 'Brian Herzlinger', 'Corey Feldman', 'Griffin Dunne', 'Samuel L. Jackson', 'Matt LeBlanc', "Bill D'Elia"],
'Griffin Dunne': ['Drew Barrymore', 'Brian Herzlinger', 'Corey Feldman', 'Eric Roberts', 'Samuel L. Jackson', 'Matt LeBlanc', "Bill D'Elia"],
'Samuel L. Jackson': ['Drew Barrymore', 'Brian Herzlinger', 'Corey Feldman', 'Eric Roberts', 'Griffin Dunne', 'Matt LeBlanc', "Bill D'Elia"],
'Matt LeBlanc': ['Drew Barrymore', 'Brian Herzlinger', 'Corey Feldman', 'Eric Roberts', 'Griffin Dunne', 'Samuel L. Jackson', "Bill D'Elia"],
"Bill D'Elia": ['Drew Barrymore', 'Brian Herzlinger', 'Corey Feldman', 'Eric Roberts', 'Griffin Dunne', 'Samuel L. Jackson', 'Matt LeBlanc'],
'Erich Anderson': ['Judie Aronson', 'Peter Barton', 'Kimberly Beck', 'Tom Everett', 'Flashlight Man', 'Corey Feldman'],
'Judie Aronson': ['Erich Anderson', 'Peter Barton', 'Kimberly Beck', 'Tom Everett', 'Flashlight Man', 'Corey Feldman'],
'Peter Barton': ['Erich Anderson', 'Judie Aronson', 'Kimberly Beck', 'Tom Everett', 'Flashlight Man', 'Corey Feldman'],
'Kimberly Beck': ['Erich Anderson', 'Judie Aronson', 'Peter Barton', 'Tom Everett', 'Flashlight Man', 'Corey Feldman'],
'Tom Everett': ['Erich Anderson', 'Judie Aronson', 'Peter Barton', 'Kimberly Beck', 'Flashlight Man', 'Corey Feldman'],
'Flashlight Man': ['Erich Anderson', 'Judie Aronson', 'Peter Barton', 'Kimberly Beck', 'Tom Everett', 'Corey Feldman']}
Note that the actor Corey Feldman will have costars from both movies.
This is the closest I have gotten. It gives me the first actor's name in list_of_actors
, repeated in a list 10 times. He appears in 10 films so it is looping through the film_cast
properly.
I have the second last line of code because I only want the co-stars to appear once, if the same 2 actors star in 2 different films together they will still only be on each others list once.
for film_cast in cast_info:
for actor in film_cast:
if list_of_actors[0] in actor['name']:
actor_name2 = actor['name']
actor_name2 != list_of_actors[0]
costars_lists[0].append(actor_name2)
One of my problems with this approach is the definition of actor_name2
. I have defined actor_name
the same way as actor_name2
in different code.
I'm not sure why my end list contains the first actor repeated 10 times as I am not appending list_of_actor[0]
, I'm trying to append the actor_name2
.
I want to check if an actor is in a film_cast
. If they are, check if all other actors are in a corresponding list, append the actors that aren't in the list already. Perform this for each film_cast
in cast_info
. Perform this for each actor in list_of_actors
.
An O(N^2)
soultion is to iterate over film cast and each actor to append film cast to an actor to co-stars mapping.
costars_dict = {}
for film_cast in cast_info:
for actor in film_cast:
costars = [cs['name'] for cs in film_cast if cs['name'] != actor['name']]
if actor['name'] in costars_dict:
costars_dict[actor['name']] += costars
else:
costars_dict[actor['name']] = costars