python 元组列表转为字典

 

#create a list
l = [("x", 1), ("x", 2), ("x", 3), ("y", 1), ("y", 2), ("z", 1)]
d = {}
for a, b in l:
    d.setdefault(a, []).append(b)
print (d)

 

posted @ 2018-11-12 10:17  anobscureretreat  阅读(982)  评论(0编辑  收藏  举报