简体   繁体   中英

Python : Adding the same elements of a list iteratively

I have a list of elements as given below-

[(a,1),(b,2),(c,3),(d,4),(e,5),(f,6),(a,7),(b,8),(c,9),(d,10),(e,11),(f,12)]

I am trying to add the value given next to a letter with a different value given next to the same letter.

For example- "a" has a value of 1, the program should compare "a" to all the terms in the list until it finds a match. Once it finds another term "a" having a value of 7, it should add the two values so that we get a=8.

Expected output is-

a=8, b=10, c=12, d=14, e=16, f=18

The solution you suggest is going to have a complexity of O(n 2 ).

You can do it in O(n) by using defaultdict (since it requires a single pass over the entire list):

from collections import defaultdict

li = [('a', 1), ('b', 2), ('c', 3), ('d', 4), ('e', 5), ('f', 6), ('a', 7), ('b', 8), 
      ('c', 9), ('d', 10), ('e', 11), ('f', 12)]

output = defaultdict(int)

for letter, number in li:
    output[letter] += number

print(output)
# defaultdict(<class 'int'>, {'a': 8, 'b': 10, 'c': 12, 'd': 14, 'e': 16, 'f': 18})

This solution of course requires the elements to be hashable, but this can be remedied by using the string representation of the elements if they are not.

An answer that doesn't utilize the defaultdict.

_list = [("a",1),("b",2),("c",3),("d",4),("e",5),("f",6),("a",7),("b",8),("c",9),("d",10),("e",11),("f",12)]

tmp = dict()

for k in _list:
    try:
        tmp[k[0]] += k[1]
    except KeyError:
        tmp[k[0]] = k[1]

print(tmp)    

Result:

{'a': 8, 'b': 10, 'c': 12, 'd': 14, 'e': 16, 'f': 18}

Using itertools.groupby and operator.itemgetter , can be substituted with lambda

from itertools import groupby
from operator import itemgetter

lst = sorted(lst, key=itemgetter(0))
d = {k: sum(i[1] for i in g) for k, g in groupby(lst, key=itemgetter(0))}
# {'a': 8, 'b': 10, 'c': 12, 'd': 14, 'e': 16, 'f': 18}

Dictionary comprehension expanded:

d = {}
for k, g in groupby(lst, key=itemgetter(0)):
    d[k] = sum(i[1] for i in g)
from collections import defaultdict

l = [('a',1),('b',2),('c',3),('d',4),('e',5),('f',6),('a',7),('b',8),('c',9),('d',10),('e',11),('f',12)]

d = defaultdict(int)
for k, v in l:
    d[k] += v

Result:

defaultdict(<class 'int'>, {'a': 8, 'b': 10, 'c': 12, 'd': 14, 'e': 16, 'f': 18})

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM