🐍 파이썬/파이썬 연습문제
[Dictionary] map key and value
써니(>_<)
2022. 7. 20. 09:11
문제: Implement a function that receives a list of Tuples, where every Tuple takes the form: (< Actor>,< Movie>,<Year>). The function should return two Dictionaries in a Tuple. The first dictionary should map from Actor to Movies, the second dictionary should map from Year to Movies.
Example
movie_maps( [("Keanu Reeves",Reeves","The Matrix", 1999 ),("Keanu Reeves", Bill Ted's Excellent Adventure", 1989)])
#({"Keanu Reeves":["The Matrix",Matrix","Bill Ted's Excellent Adventure"]}, { 1999 :["The Matrix"], 1989:["Bill Ted's Excellent Adventure"]})
def movie_maps(l):
actor_dic = {}
year_dic = {}
for t in l:
a, m, y = t
if a not in actor_dic:
actor_dic[a] =[]
if y not in year_dic:
year_dic[y] = []
actor_dic[a].append(m)
year_dic[y].append(m)
return actor_dic, year_dic