Sep-26-2020, 06:01 PM
(This post was last modified: Sep-26-2020, 06:01 PM by pythonnewbie138.)
Hi everyone,
The class I'm writing queries TMDB and, for now, writes data to a txt file. I've written a method to populate global variables to use later but I need a way to organize the same type of data for multiple people in a single list. ie. Dictionaries inside a list.
Below is the piece of code that I'm struggling with. The method populates an existing list with specific pieces of information to use in other tasks later. What I want to do create dictionaries named
Desired data structure:
*NOTE* I kept the indentation as-is from the code because this is a block inside a class method.
The class I'm writing queries TMDB and, for now, writes data to a txt file. I've written a method to populate global variables to use later but I need a way to organize the same type of data for multiple people in a single list. ie. Dictionaries inside a list.
Below is the piece of code that I'm struggling with. The method populates an existing list with specific pieces of information to use in other tasks later. What I want to do create dictionaries named
j['name']
inside list director_known_for
and add the rest of the existing .append statement as the value of an increasing key series =. ie. inc=1 movie[inc] inc +=1
incrementing. Desired data structure:
director_known_for = [director1: {movie1:{k['title'] + " " + k['release_date'] + " " + "https://www.themoviedb.org/movie/" + str(k['id'])}, movie2:k['title'] + " " + k['release_date'] + " " + "https://www.themoviedb.org/movie/" + str(k['id'])}, director2:{movie1:k['title'] + " " + k['release_date'] + " " + "https://www.themoviedb.org/movie/" + str(k['id'])}, movie2:k['title'] + " " + k['release_date'] + " " + "https://www.themoviedb.org/movie/" + str(k['id'])},Sorry if my dict formatting is a little off. This is the first time I'm trying to write to/create dictionaries :)
*NOTE* I kept the indentation as-is from the code because this is a block inside a class method.
director_known_for = [] if bool(self.director_known_for): # Add organization and output for multiple directors. maybe a list containing dicts? for i in directors: director_search = search.person(query=i) for j in director_search['results']: for k in j['known_for']: director_known_for.append(j['name'] + " " + k['title'] + " " + k['release_date'] + " " + "https://www.themoviedb.org/movie/" + str(k['id']))Any help is greatly appreciated!