2
# ****************************************************************************************************** # Data is imported from the included awards_data.py file. Both files must remain in the same directory. # Do not modify the code in this section. # You may not hardcode (manually enter in the code) any other data. from awards_data import SAG, oscars, NBR, ISA, GLAAD, NAACP award_list_names = ['sag', 'oscars', 'nbr', 'isa', 'glaad', 'naacp'] award_list_options = [SAG, oscars, NBR, ISA, GLAAD, NAACP] # ****************************************************************************************************** 

Program has to ask what list you want to print from the list of lists (this)

award_list_options = [SAG, oscars, NBR, ISA, GLAAD, NAACP] 

so if the user inputs sag, it should print the values of SAG from the original list of SAG which was imported before

this is the awards_data file if it is relevant

SAG = ["1", "2"] oscars = ["1", "3", "4", "5", "3", "1", "6", "7", "3", "8", "1", "32", "3", "2"] NBR = ["2", "9", "3", "10", "Close", "Sr.", "6"] ISA = ["1", "11", "12", "1", "13", "Tár", "1"] GLAAD = ["15", "16", "17", "18", "19"] NAACP = ["8", "20", "16", "21", "22"] 

for input sag the output should be:

1,2 

for input oscars, OSCARS, oscarS or with whitespace should be

1,3,4,5,3,1,6,7,3,8,1,32,3,2 all separated with new line 
4
  • 1
    Does this answer your question? What is the most efficient way to search nested lists in python? Commented Oct 20, 2023 at 9:27
  • @Daviid No it doesn't, because I am not looking for the lists individual values first but I am looking for when/if the string input is inside of the group of lists (award_list_options) and then when it does match I would like to print all the values inside of that matching list name Commented Oct 20, 2023 at 9:35
  • Actually in your code you just print('asd') not all the values inside of that matching list name. Commented Oct 20, 2023 at 9:40
  • Yes I tried printing asd first instead of the values, to make it easier for now. But it has yet to work so I couldn't progress. asd is just a placeholder for now. But yes, how can I do both? Commented Oct 20, 2023 at 9:42

3 Answers 3

1

There are a lot of errors in your code, you're either not posting you code as you have it, which makes any help we give you useless because we're not on the same page, or you're just ignoring the interpeter errors.

  • First of all, you pass nothing to print_award_winners so fix that.
  • Second, in Award_Selection.(lower).strip() Award_Selection might be picked up as global variable, it's still best to use arguments for this, also, to make a string lowercase it's 'string'.lower() like with strip
  • Lastly that print_award_winners() call is indented and python will complain, so fix that too.

Here's the cleaned up code.

def print_award_winners(winner): winner = winner.lower().strip() if winner in award_list_options: for movie in award_list_options[winner]: print(movie) award_list_options = { "sag": ["1", "2"], "oscars": ["1", "3", "4", "5", "3", "1", "6", "7", "3", "8", "1", "32", "3", "2"], "nbr": ["2", "9", "3", "10", "Close", "Sr.", "6"], "isa": ["1", "11", "12", "1", "13", "Tár", "1"], "glaad": ["15", "16", "17", "18", "19"], "naacp": ["8", "20", "16", "21", "22"], } Award_Selection = input("Enter winner: ") print_award_winners(Award_Selection) 

Latest Code:

# ****************************************************************************************************** # Data is imported from the included awards_data.py file. Both files must remain in the same directory. # Do not modify the code in this section. # You may not hardcode (manually enter in the code) any other data. from awards_data import SAG, oscars, NBR, ISA, GLAAD, NAACP award_list_names = ['sag', 'oscars', 'nbr', 'isa', 'glaad', 'naacp'] award_list_options = [SAG, oscars, NBR, ISA, GLAAD, NAACP] # ****************************************************************************************************** def print_award_winners(winner): winner = winner.lower().strip() if winner in name_to_list_relations: for movie in name_to_list_relations[winner]: print(movie) name_to_list_relations = dict(zip(award_list_names, award_list_options)) Award_Selection = input("Enter winner: ") print_award_winners(Award_Selection) 

Learn about zip() here and here

Sign up to request clarification or add additional context in comments.

19 Comments

It still doesn't seem to give me any values using this code. This is only a part of the code and can't post the whole code since this is a school project and don't want to post my whole project here due to Academic Misconduct. Although I did post everything needed here.
If you have values like "Close" and "Sr." with upper case letters then you shouldn´t do .lower() because it'll never match those, unless you also want to lower every element in the sublists.
That's why I would want to somehow match sag to SAG and just print SAG as a whole. so the input can be sag, but it should know it meant SAG and prints everything inside SAG I don't know If I did explain it correctly
Problem is that I cannot match sag into SAG I can make it .upper() but then oscars list would not work since that is lower case
It can be done, but it's ugly af code using eval() and since you said this is a school project it's obviously not the answer, you've misunderstood the assignment clearly.
|
0

Quick example with using a dictionary, not sure if it matches what your "assignment" wants but it should answer your original question.

import awards_data award_list_options = { "SAG": awards_data.SAG, "oscars": awards_data.oscars, "NBR": awards_data.NBR, "ISA": awards_data.ISA, "GLAAD": awards_data.GLAAD, "NAACP": awards_data.NAACP } def print_award_winners(winner): for award, options in award_list_options.items(): if winner.lower().strip() in map(str.lower, options): print(award) print_award_winners('1') # SAG # oscars # ISA 

Not quite sure how they expect you to print out "SAG" from the variable SAG but it works if you just hardcode the options. Maybe they just meant for you to learn if statements (ie. if '1' in SAG: print('SAG')).

2 Comments

We arent allowed to change award_list_options list
Updated to not change it, but yeah it doesn't sound like a "good" assignment.
0

You need an auxiliary data structure

names = [n.lower() for n in ["SAG", "oscars", "NBR", "ISA", "GLAAD", "NAACP"]] 

then

def p(): s = input('Prompt: ')).lower().strip() if s in names: print(*award_list_options[names.index(s)] sep=', ') else: print('No Matches') 

Even better, use a dictionary.

Comments