how to aggregate elements of a list of tuples if the tuples have the same first element?

后端 未结 4 719
日久生厌
日久生厌 2021-02-13 19:01

I have a list in which each value is a list of tuples. for example this is the value which I extract for a key :

     [(\'1998-01-20\',8) , (\'1998-01-22\',4) ,         


        
4条回答
  •  野性不改
    2021-02-13 19:26

    Yet another answer different from the ones given already. You can simpy create a new dictionary where the keys are the year-month combinations. A loop over the dates in your list + using dictionary.get(key, defaultvalue) should do the trick. IT adds the current value to the value in the new dictionary, if the key did not yet exist, it returns the default value 0 and creates the key.

    data = [('1998-01-20',8) , ('1998-01-22',4) , ('1998-06-18',8 ) , ('1999-07-15' , 7), ('1999-07-21',1)]
    dictionary = dict()
    for (mydate, val) in data: #
        ym = mydate[0:7]    # the key is only the year month combination (i.e. '1998-01' for example)
        dictionary[ym] = dictionary.get(ym, 0) + val  # return the value for that key or return default 0 (and create key)
    
    data_aggregated = [(key, val) for (key, val) in dictionary.iteritems()] # if you need it back in old format
    

提交回复
热议问题