Summing values in nested list when item changes

后端 未结 5 1163
眼角桃花
眼角桃花 2021-01-23 07:45

I have some problem which has stumped my beginner-knowledge of python and I hope someone out there can point me in the correct direction.

I generated a nested list, each

相关标签:
5条回答
  • 2021-01-23 08:02

    Here's a variation on m170897017's technique:

    a = [[1, 0],[1, 2],[2, 9],[3, 0],[3, 8],[3, 1]]
    
    result = {}
    for day, val in a:
        if day not in result:
           result[day] = 0
        result[day] += val
    
    print result
    
    #Convert back into a list
    print [list(t) for t in result.items()]
    

    output

    {1: 2, 2: 9, 3: 9}
    [[1, 2], [2, 9], [3, 9]]
    

    If you're using Python 2.7 or later, you could also use a Counter.

    Another possibility is to use a defaultdict, which has been available since Python 2.5.

    from collections import defaultdict
    
    a = [[1, 0],[1, 2],[2, 9],[3, 0],[3, 8],[3, 1]]
    
    result = defaultdict(int)
    for day, val in a:
        result[day] += val
    
    print [list(t) for t in result.items()]
    

    output

    [[1, 2], [2, 9], [3, 9]]
    
    0 讨论(0)
  • 2021-01-23 08:03

    You can put the key in a dictionary and store the counts in the values. Like this:

    #!/usr/bin/python
    # -*- coding: utf-8 -*-
    
    a = [[1, 0],[1, 2],[2, 9],[3, 0],[3, 8],[3, 1]]
    res = {}
    for i in a:
        if i[0] in res:
            res[i[0]] += i[1]
        else:
            res[i[0]] = i[1]
    
    print res
    

    OUTPUT:

    {1: 2, 2: 9, 3: 9}
    

    This output is in dictionary format. You can turn it to list format as you like.

    0 讨论(0)
  • 2021-01-23 08:06

    You can use a collections.OrderedDict mapping items to list:

    l = [[1, 0],[1, 2],[2, 9],[3, 0],[3, 8],[3, 1]]
    
    from collections import OrderedDict
    d = OrderedDict()
    
    for a, b in l:
        d.setdefault(a, 0)
        d[a] += b
    print(map(list,d.iteritems()))
    
    [[1, 2], [2, 9], [3, 9]]
    
    0 讨论(0)
  • 2021-01-23 08:09

    Problem's like this calls for itertools.groupby. Particularly groupby, groups consecutive values with same keys, where keys can be specified by user. Nevertheless, in this case, key is trivial enough as indexing a particular element of a list, so that should be a reason enough to use operator.itemgetter. Finally, you can wrap up either as a functional (using map/imap) or as a generator expression based on your taste and choice.

    >>> from itertools import groupby, imap
    >>> from operator import itemgetter
    >>> lst=[[1, 0],[1, 2],[2, 9],[3, 0],[3, 8],[3, 1]]
    >>> [[k, sum(imap(itemgetter(1), v))]
         for k, v in groupby(lst,key = itemgetter(0))]
    [[1, 2], [2, 9], [3, 9]]
    
    0 讨论(0)
  • 2021-01-23 08:09

    You can try using defaultdict ...

    from collections import defaultdict
    
    dat = [[1, 0],[1, 2],[2, 9],[3, 0],[3, 8],[3, 1]]
    
    d = defaultdict(int)
    for k,v in dat: d[k] += v
    
    0 讨论(0)
提交回复
热议问题