Converting list of tuples into a dictionary

后端 未结 7 1094
温柔的废话
温柔的废话 2020-11-27 05:52

I\'m looking for a way to convert a list of tuples like this:

[(1,4),(2,4),(3,4),(4,15),(5,15),(6,23),(7,23),(8,23),(9,15),(10,23),(11,15),(12,15)]


        
相关标签:
7条回答
  • 2020-11-27 06:15
    l = [(1,4),(2,4),(3,4),(4,15),(5,15),(6,23),(7,23),(8,23),(9,15),(10,23),(11,15),(12,15)]
    d = {}
    for v, k in l:
        d.setdefault(k, []).append(v)
    
    0 讨论(0)
提交回复
热议问题