Python Forum
Dictionary/List Homework
Thread Rating:
  • 1 Vote(s) - 5 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Dictionary/List Homework
#1
Hello all!!

I am currently working on a homework assignment where I need to get information from a dictionary based off of a list. Example:
dict = {'title':[year, 'name'],....etc
list = ['name','name','name'].....etc

What I need to do is get the name from list to match the name in the dictionary and then print the values from the dictionary.
movies = {"Munich":[2005, "Steven Spielberg"],
        "The Prestige": [2006, "Christopher Nolan"],
        "The Departed": [2006, "Martin Scorsese"],
        "Into the Wild": [2007, "Sean Penn"],
        "The Dark Knight": [2008, "Christopher Nolan"],
        "Mary and Max": [2009, "Adam Elliot"],
        "The King\'s Speech": [2010, "Tom Hooper"],
        "The Help": [2011, "Tate Taylor"],
        "The Artist": [2011, "Michel Hazanavicius"],
        "Argo": [2012, "Ben Affleck"],
        "12 Years a Slave": [2013, "Steve McQueen"],
        "Birdman": [2014, "Alejandro G. Inarritu"],
        "Spotlight": [2015, "Tom McCarthy"],
        "The BFG": [2016, "Steven Spielberg"]}

options = input('Choose a sort option: \n')
director_list = []
if options == 'd':
    for key in movies:
        i = 1
        while i < len(movies[key]):
            director_list.append(movies[key][i])
            i += 2
set_director = sorted(set(director_list))
for key in set_director:
    print('%s:\n' % key)
    i = 0
    for key, value in sorted(movies.items(), key=lambda item: (item[0], item[1])):
        i = 1
        while i < len(movies[key]):
            print("\t" + key + ',',movies[key][0])
            i += 2
Reply
#2
def get_author(data, search_author):
    # using here argument unpacking
    for movie, (year, author) in data.items():
        if search_author == author:
            print(movie, year, author)


get_author(movies, 'Steven Spielberg')
Without argument unpacking:

def get_author(data, search_author):
    # using here tuple unpacking
    for movie, value in data.items():
        year = value[0]
        author = value[1]
        if search_author == author:
            print(movie, year, author)


get_author(movies, 'Steven Spielberg')
If you want to sort them by X, use the sorted builtin function together with the key argument.
def by_author(item):
    '''
    Returns the author of an item
    '''
    return item[1][1]


def by_year(item):
    '''
    Returns the year of an item
    '''
    return item[1][0]


def by_movie(item):
    '''
    Returns the movie of an item
    '''
    return item[0]


def get_author_sorted(data, search_author, sort_by):
    for movie, (year, author) in sorted(data.items(), key=sort_by):
        if search_author == author:
            print(movie, year, author)


get_author_sorted(movies, 'Steven Spielberg', by_year)
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
Reply
#3
Thank you
Reply
#4
While working on this, I have it working as it should with one exception. Under the dictionary, there is two list that has 4 values instead of 2 values. When I run the program it isn't printing the right values together for just those two year. I have messed with this for days and can't seem to figure out how to get it to print correctly.
movies = {2005: ['Munich', 'Steven Spielberg'],
              2006: ['The Prestige', 'Christopher Nolan', 'The Departed', 'Martin Scorsese'],
              2007: ['Into the Wild', 'Sean Penn'],
              2008: ['The Dark Knight', 'Christopher Nolan'],
              2009: ['Mary and Max', 'Adam Elliot'],
              2010: ['The King\'s Speech', 'Tom Hooper'],
              2011: ['The Artist', 'Michel Hazanavicius', 'The Help', 'Tate Taylor'],
              2012: ['Argo', 'Ben Affleck'],
              2013: ['12 Years a Slave', 'Steve McQueen'],
              2014: ['Birdman', 'Alejandro G. Inarritu'],
              2015: ['Spotlight', 'Tom McCarthy'],
              2016: ['The BFG', 'Steven Spielberg']}

options = input('Choose a sort option: \n')
title_list = []
if options == 't':
    for key in movies:
        i = 0
        while i < len(movies[key]):
            title_list.append(movies[key][i])
            i += 2
    set_title = sorted(set(title_list))
    for title in set_title:
        print('%s:' % title)
        for year, value in sorted(movies.items()):
            if title in value:
                print('\t%s, %s' % (value[value.index(title)-1], year))
        print()
Reply
#5
Your dictionary is messed up. Should be separate sub-lists
2011: [['The Artist', 'Michel Hazanavicius'], ['The Help', 'Tate Taylor']], 
Reply
#6
(Dec-12-2018, 05:31 PM)woooee Wrote: Your dictionary is messed up. Should be separate sub-lists
2011: [['The Artist', 'Michel Hazanavicius'], ['The Help', 'Tate Taylor']], 

If I change that then none of the program runs correctly.
Reply
#7
Not enough info to go further. Is this dictionary done, or do you want to add to it later?
Reply
#8
It's done.

I have to sort the dictionary based off of user input.
q = quit
d = director
y = year
t = title

I have the year code and I have the director code. The title sort isn't working out so well. Director and title have to be sorted in alphabetical order. Which is why I made a list from the values in the dictionary so I could sort them and then use them as a value to reference the dictionary key and elements.
Reply
#9
(Dec-12-2018, 08:04 PM)ImLearningPython Wrote: If I change that then none of the program runs correctly.
Then fix the program. Instead of having a list of movies/titles where every other element has meaning, just make each year a list of lists, as was shown. It makes sense logically, looks better, is easier to update, and the code that parses it will still be simple.
Reply
#10
(Dec-12-2018, 08:41 PM)nilamo Wrote: Instead of having a list of movies/titles where every other element has meaning, just make each year a list of lists, as was shown. It makes sense logically, looks better, is easier to update, and the code that parses it will still be simple.

i.e

2005: [['Munich', 'Steven Spielberg']],
2006: [['The Prestige', 'Christopher Nolan'], ['The Departed', 'Martin Scorsese']], 
etc.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Help with list homework eyal123 5 1,642 Nov-18-2022, 03:46 PM
Last Post: deanhystad
  Homework - List containing tuples containing dicti Men 4 2,015 Dec-28-2021, 12:37 AM
Last Post: Men
  Sorting list - Homework assigment ranbarr 1 2,229 May-16-2021, 04:45 PM
Last Post: Yoriz
  Loop through elements of list and include as value in the dictionary Rupini 3 2,648 Jun-13-2020, 05:43 AM
Last Post: buran
  How can details be dynamically entered into a list and displayed using a dictionary? Pranav 5 2,928 Mar-02-2020, 10:17 AM
Last Post: buran
  Functions returns content of dictionary as sorted list kyletremblay15 1 2,039 Nov-21-2019, 10:06 PM
Last Post: ichabod801
  have homework to add a list to a list using append. celtickodiak 2 2,010 Oct-11-2019, 12:35 PM
Last Post: ibreeden
  Dictionary Homework beepBoop123 3 2,602 Dec-11-2018, 10:00 PM
Last Post: buran
  making a dictionary from a list, one key with multiple values in a list within a list rhai 4 3,598 Oct-24-2018, 06:40 PM
Last Post: LeSchakal
  Need some help with list and dictionary .txt GeekLife97 3 3,823 Jan-20-2017, 08:00 AM
Last Post: wavic

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020