Converting list of tuples into a dictionary

后端 未结 7 1153
温柔的废话
温柔的废话 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:03

    for key, value in tuples:
        if d.get(key):
            d[key].append(value)
            continue
        d[key] =[value]
    

提交回复
热议问题