:
, :
a=[(2006,1),(2007,4),(2008,9),(2006,5)]
dict . - :
{2008: [9], 2006: [5], 2007: [4]}
, , , , , (2006,1) (2006,5), , . , :
{2008: [9], 2006: [1, 5], 2007: [4]}
- :
dict, :
if item[0] not in new_dict:
new_dict[item[0]]=[item[1]]
else:
new_dict[item[0]].append(item[1])
, , dict, :
:
a=[(2006,1),(2007,4),(2008,9),(2006,5)]
new_dict={}
for item in a:
if item[0] not in new_dict:
new_dict[item[0]]=[item[1]]
else:
new_dict[item[0]].append(item[1])
print(new_dict)
, - , :
elif userChoice=="y":
new={}
for key, value in sorted(movieCollectionDict.items()):
for k in value:
if value[0] not in new:
new[value[0]]=[(key,value[1])]
else:
new[value[0]].append((key,value[1]))
print({key: set(value) for key, value in new.items()})
, :
def main():
movieCollectionDict = {"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\ 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"]}
promptForYear = True
while promptForYear:
yearChoice = int(input("Enter a year between 2005 and 2016:\n"))
if yearChoice<2005 or yearChoice>2016:
print("N/A")
else:
for key, value in movieCollectionDict.items():
if value[0] == yearChoice:
print(key + ", " + str(value[1]) )
promptForYear = False
menu = "MENU" \
"\nSort by:" \
"\ny - Year" \
"\nd - Director" \
"\nt - Movie title" \
"\nq - Quit\n"
promptUser = True
while promptUser:
print("\n" + menu)
userChoice = input("Choose an option:\n")
if userChoice == "q":
promptUser = False
elif userChoice=="y":
new={}
for key, value in sorted(movieCollectionDict.items()):
for k in value:
if value[0] not in new:
new[value[0]]=[(key,value[1])]
else:
new[value[0]].append((key,value[1]))
print({key: set(value) for key, value in new.items()})
elif userChoice == "d":
for key, value in sorted(movieCollectionDict.items(), key=lambda key_value: key_value[1][1]):
print(value[1],end=':\n')
print("\t" + key + ", " + str(value[0])+"\n")
elif userChoice == "t":
for key, value in sorted(movieCollectionDict.items(), key=lambda item: (item[0], item[1])):
print(key,end=':\n')
print("\t" + str(value[1]) + ", " + str(value[0])+"\n")
else:
print("Invalid input")
main()
P.S: , , , () ( ) .