Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Python: deleting dictionaries from a list I'm trying to delete dictionaries from a list that fit my parameters. Here is my current code, I've commented

Python: deleting dictionaries from a list

I'm trying to delete dictionaries from a list that fit my parameters.

Here is my current code, I've commented where I'm having a problem.

This code works from the movie.txt file located here: http://s000.tinyupload.com/?file_id=48953557772434487729

Basically the program takes the info from the txt file and then converts the lists of each file into a dicitonary and puts them into a list. From this list, I'm trying to sort out and exclude the things I don't need. I thought my sytax was right, but apparently not.

I only included neccessary code to cut down on the clutter. I am having probems at the "action" point and excluding and deleting the dictionaries there. When prompted enter "s" and then "a" to access those two menus.

def load_movies(): global movies_list movies_list= [] file_ref = open("movies.txt", 'r') line = file_ref.readline() for line in file_ref: line = line.strip() current = {} if line == '': break  movie_data = line.split("\t") current["title"] = movie_data[0] current["year"] = movie_data[1] current["length"] = movie_data[2] current["rating"] = movie_data[3] current["action"] = int(movie_data[4][0]) == 1 current["animation"] = int(movie_data[4][1]) == 1 current["comedy"] = int(movie_data[4][2]) == 1 current["drama"] = int(movie_data[4][3]) == 1 current["documentary"] = int(movie_data[4][4]) == 1 current["romance"] = int(movie_data[4][5]) == 1 movies_list.append(current) del current file_ref.close() def menu(): movie_selector =("Movie Selector - Please enter an option below: L - List all movies Y - List all movies by year "  "T - Search by title S - Search by genre, rating, and maximum length Q - Quit the program Option:") movie_selector_input = input(movie_selector).upper() if movie_selector_input == "L": list_movies() if movie_selector_input == "Y": list_by_year() if movie_selector_input == "T": search_by_title() if movie_selector_input == "S": search() if movie_selector_input == "Q": print("Thanks for using my program! Goodbye.") exit() else: print("Invalid input") print("Please try again") print() return menu() def search(): genre_input = input("Please make a selection from the following genres. (Action(A), Animation(N), Comedy(C), "  "Drama(D), Documentary(O), or Romance(R)):").lower() if genre_input == 'a': action_genre() elif genre_input == 'n': animation_genre() elif genre_input == 'c': comedy_genre() elif genre_input == 'd:': drama_genre() elif genre_input == 'o': documentary_genre() elif genre_input == 'r': romance_genre() else: print("Invalid genre") print() menu() #this is where I can't get the syntax to work def action_genre(): for current in movies_list: if current["action"] == "False": del current break  for i in movies_list: print(i) 

Each dictionary in the list looks like this:

image text in transcribed

Thank you again for help! I can't figure this out!

For example, from the dataset, the information of the first movie is: $ 1971 121 NR 001100 A dictionary below should be built for this movie: {"Title": "S", "Year": 1971, "Length": 121, "Rating": "NR", "Action": False, "Animation": False, "Comedy" True, "Drama": True, "Documentary" False, "Romance": False)

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

JDBC Database Programming With J2ee

Authors: Art Taylor

1st Edition

0130453234, 978-0130453235

More Books

Students also viewed these Databases questions