0

我有一些这样的清单

[ ['2000-01-01', 1.0], ['2000-02-01', 2.0] ]
[ ['2000-01-01', 2.0], ['2000-02-01', 1.0], ['2000-03-01', 3.0] ]
[ ['2000-01-01', 3.1], ['2000-02-01', 2.0], ['2000-03-01', 1.4] ]

我如何合并数据时间并对值求和得到这样的列表

[ ['2000-01-01', 6.1], ['2000-02-01', 5.0], ['2000-03-01', 4.4] ]

列表中元素的类型是 [datetime, double]

4

4 回答 4

1

在这里使用collections.defaultdict()。见片段。

>>> a = [ ['2000-01-01', 1.0], ['2000-02-01', 2.0] ]
>>> b = [ ['2000-01-01', 2.0], ['2000-02-01', 1.0], ['2000-03-01', 3.0] ]
>>> c = [ ['2000-01-01', 3.0], ['2000-02-01', 2.0], ['2000-03-01', 1.0] ]
>>> from collections import defaultdict
>>> m = defaultdict(int)
>>> d = a + b + c
>>> d
[['2000-01-01', 1.0], ['2000-02-01', 2.0], ['2000-01-01', 2.0], ['2000-02-01', 1.0], ['2000-03-01', 3.0], ['2000-01-01', 3.0], ['2000-02-01', 2.0], ['2000-03-01', 1.0]]
>>> for date, count in d:
        m[date] += count
>>> m.items()
[('2000-02-01', 5.0), ('2000-03-01', 4.0), ('2000-01-01', 6.0)]
于 2013-07-30T03:06:37.953 回答
0

对于所有类似 Excel 的编程,我建议使用 Pandas,因为它是为此而设计的。但是,如果您想继续使用标准库,那么 Counter 就足够了。

于 2013-07-30T03:09:53.327 回答
0

使用counter.Counter

from collections import Counter

result = Counter()
result += Counter(dict([ ['2000-01-01', 1], ['2000-02-01', 2] ]))
result += Counter(dict([ ['2000-01-01', 2], ['2000-02-01', 1], ['2000-03-01', 3] ]))
result += Counter(dict([ ['2000-01-01', 3], ['2000-02-01', 2], ['2000-03-01', 1] ]))
result.most_common() # => [('2000-01-01', 6), ('2000-02-01', 5), ('2000-03-01', 4)]
于 2013-07-30T03:10:11.277 回答
0

使用计数器

#inputList is List containg 
from collections import Counter
from operator import itemgetter

def mergeAndGetSum(inputList):
    cnt = Counter()

    #get counts for each date
    for list1 in inputList:
        for list2 in list1:
            cnt[list2[0]] += list2[1]
    ret = []

    #creaet the output list
    for key in cnt:
        ret.append([key,cnt[key]])

    #sort output list based on dates
    ret.sort(key=itemgetter(0))
    return ret


if __name__ == "__main__":
    listOfLists = [
            [ ['2000-01-01', 1], ['2000-02-01', 2] ],
            [ ['2000-01-01', 2], ['2000-02-01', 1], ['2000-03-01', 3] ],
            [ ['2000-01-01', 3], ['2000-02-01', 2], ['2000-03-01', 1] ]
            ]
    ret = mergeAndGetSum(listOfLists)
    print ret
于 2013-07-30T03:31:06.077 回答