I am trying to create a list of all actors' co-actors. cast_info is a list of lists. The lists contain dictionaries. The dictionaries have one key = 'name'. I made a list_of_actors so I could try and solve the problem for 1 actor - it's just a simple list of 54000 actors. I will attempt to generalise once I get the right end list for the first actor.
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)
Any help is much appreciated
One of my problems 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.
what I currently get as output but would like a list of all of 'Sam Worthington' co-stars
CodePudding user response:
WIP
Is this what you are looking for? Assuming the input is
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 code is
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
you can get a mapping of actors and lists of their costars. costars_dict
will be then (note that the actor Corey Feldman will have costars from both movies):
{'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']}
Is this what you need?