sum each value in a list of tuples

后端 未结 3 875
醉话见心
醉话见心 2020-12-01 10:30

I have a list of tuples similar to this:

l = [(1, 2), (3, 4), (5, 6), (7, 8), (9, 0)]

I want to create a simple one-liner that will give me

相关标签:
3条回答
  • 2020-12-01 11:00

    Use zip() and sum():

    In [1]: l = [(1, 2), (3, 4), (5, 6), (7, 8), (9, 0)]
    
    In [2]: [sum(x) for x in zip(*l)]
    Out[2]: [25, 20]
    

    or:

    In [4]: map(sum, zip(*l))
    Out[4]: [25, 20]
    

    timeit results:

    In [16]: l = [(1, 2), (3, 4), (5, 6), (7, 8), (9, 0)]*1000
    
    In [17]: %timeit [sum(x) for x in zip(*l)]
    1000 loops, best of 3: 1.46 ms per loop
    
    In [18]: %timeit [sum(x) for x in izip(*l)]       #prefer itertools.izip
    1000 loops, best of 3: 1.28 ms per loop
    
    In [19]: %timeit map(sum, zip(*l))
    100 loops, best of 3: 1.48 ms per loop
    
    In [20]: %timeit map(sum, izip(*l))                #prefer itertools.izip
    1000 loops, best of 3: 1.29 ms per loop
    
    0 讨论(0)
  • 2020-12-01 11:01

    Without using zip

    sum(e[0] for e in l), sum(e[1] for e in l)
    
    0 讨论(0)
  • 2020-12-01 11:11

    I want to add something to the given answer:

    If I have an array of dict e.g.

    l = [{'quantity': 10, 'price': 5},{'quantity': 6, 'price': 15},{'quantity': 2, 'price': 3},{'quantity': 100, 'price': 2}]
    

    and i want to obtain two (or more) sums of calculated quantity over the values e.g. sum of quantities and of price*quantity

    I can do:

    (total_quantity, total_price) = (
    sum(x) for x in zip(*((item['quantity'],
                           item['price'] * item['quantity'])
                          for item in l)))
    

    Instead of:

    total_quantity = 0
    total_price = 0
    for item in l:
         total_quantity += item['quantity']
         total_price += item['price'] * item['quantity']
    

    Maybe the first solution is less readable, but is more "pythonesque" :)

    0 讨论(0)
提交回复
热议问题