简体   繁体   中英

How can I print two dict values with the same key from one dict?

I'm organizing a list of movie titles using Python for an academic assignment. The submission deadline has already passed; I just want to understand how it works for the future.

Here's the code:

movies = {

2005: ['Munich', 'Steven Spielberg'],
2006: ['The Prestige', 'Christopher Nolan'],
2006: ['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'],
2011: ['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']

}

userInput = int(input('Enter a year between 2005 and 2016: \n'))

print(movies[userInput])

The print statement works fine if I choose a year with only one entry. If the user inputs '2006' two titles should show up, but I'm only getting the latter value for 2006 key. What am I doing wrong?

Keys must be unique to a dictionary. Therefore, if you add an entry to a dictionary, it will overwrite any entry that used the same key.

If you insisted on using years as key (don't know if your assignment requires this for some bizarre reason), it would be possible to put entries in a list like

{
   2005: [('Munich', 'Steven Spielberg'), ('King Kong', 'Peter Jackson')],
   ...
}

but that requires some additional techniques such as using setdefault() or a defaultdict (or manually checking whether a key already exists before adding a new item).

Note that in my example, I used a list for collections of similar elements and tuples for collections of different elements (title/director). This may be a good habit to adopt.

You can reorganize your dictionary like this:

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')]
}

A movie is a tuple (title, director) , and a year entry is a list of movies. Choosing a year gives you the list of movies,

>>> print(movies[2006])
[('The Prestige', 'Christopher Nolan'), ('The Departed', 'Martin Scorsese')]

and you can then extract the titles or directors by iterating through the list.

>>> print([movie[0] for movie in movies[2006]])
['The Prestige', 'The Departed']

>>> print([movie[1] for movie in movies[2006]])
['Christopher Nolan', 'Martin Scorsese']

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM