I have an input file that I am trying to build a dictionary from

Sarah De-Paz

I have an input file that I am trying to build a data base from.
Each line looks like this:

Amy Shchumer, Trainwreck, I Feel Pretty, Snatched, Inside Amy Shchumer  
Bill Hader,Inside Out,  Trainwreck, Tropic Thunder 

And so on.
The first string is an actor\actress, and then movies they played in.
The data isn't sorted and they are some trailing whitespaces.

I would like to create a dictionary that would look like this:
{'Trainwreck': {'Amy Shchumer', 'Bill Hader'}}
The key would be the movie, the values should be the actors in it, unified in a set data type.

def create_db():
   my_dict = {}
   raw_data = open('database.txt','r+') 
   for line in raw_data:
      lst1 = line.split(",") //to split by the commas 
      len_row = len(lst1)
      lst2 = list(lst1) 
      for j in range(1,len_row):
         my_dict[lst2[j]] = set([lst2[0]])
print(my_dict)

It doesn't work... it doesn't solve the issue that when a key already exists then the actor should be unified in a set with the prev actor

Instead I end up with:
'Trainwreck': {'Amy Shchumer'}, 'Inside Out': {'Bill Hader'}

rassar
def create_db():
    db = {}
    with open("database.txt") as data:
        for line in data.readlines():
            person, *movies = line.split(",")
            for m in movies:
                m = m.strip()
                db[m] = db.get(m, []) + [person]

    return db

Output:

{'Trainwreck': ['Amy Shchumer', 'Bill Hader'], 
 'I Feel Pretty': ['Amy Shchumer'], 
 'Snatched': ['Amy Shchumer'], 
 'Inside Amy Shchumer': ['Amy Shchumer'], 
 'Inside Out': ['Bill Hader'], 
 'Tropic Thunder': ['Bill Hader']}

This will loop through the data and assign the first value of each line to person and the rest to movies (see here for an example of how * unpacks tuples). Then for all the movies, it uses .get to check if it’s in the database yet, returning the list if it is and an empty list if it isn’t. Then it adds the new actor to the list.

Another way to do this would be to use a defaultdict:

from collections import defaultdict

def create_db():
    db = defaultdict(lambda: [])
    with open("database.txt") as data:
        for line in data.readlines():
            person, *movies = line.split(",")
            for m in movies:
                db[m.strip()].append(person)

    return db

which automatically assigns [] if the key does not exist.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

I am new to python and I am trying to build a simple Tic-Tac-Tock game.When I am taking input from user it goes to infinity loop

I am trying to display the index value of strings which I have obtained using the input function

I am trying to match an integer I have stored on a file that I loop through with a value I have stored in variable that is constantly changing

I'm trying to clean up a script I have by trying to make it build the folder structure based on the file name

I am trying to print information from a json file with python

I am trying to build a simple calculator

I am trying to build and install this driver but failing

I am trying to get the output in below nested dictionary format for the given input

I am trying to read a file and to show the reading progress in %. I have tried following code but I am not able to divide in chunks & show progress

I am trying to access the video from the Document directory and i am getting error "No such file or directory"

I am trying to get tokens from a file and make a linked list. But I am failing at it

I am trying to write a shell script to read username from a file, but it is not working. I am posting script I am writing and output

I have a file called "¬" and I am confused

Hi, I am trying to input data from nasdaq into R and am getting an error

I am new to golang, I am trying to create a zip that must have a folder inside that is: tosend.zip/archivos/file.png

Why am I getting a KeyError when trying to use an Enum as a dictionary key in another file?

Trying to turn a .txt file into a dictionary of words. Where am I going wrong? (C#)

How can I take a list input from a file and turn it into a dictionary?

I am trying to print all of the logged-in users tasks from a file(tasks.txt). This is what I have so far but it only prints out one task

I am trying to write a code in python to get the values from a multiline string and store it in dictionary

I am trying to add entries from list in list to dictionary present in other list

I have an appointment scheduling application I am trying to create

I am trying to write an equation in LaTeX and i have an error

I am trying to search for a file in sharepoint

I am trying to implement CORS to my JavaScript code correctly, and it worked for a while, I believe I am missing some file from the project?

I am trying to convert csv into xlsx fileI am getting an error "No columns to parse from file"

I am trying to retrieve data from a JSON file, but I am having trouble retrieving information from a specific property

I am trying to build BST in python from scratch RecursionError: maximum recursion depth exceeded in comparison

I am trying to open a modal from a scrollview