Adding The Sorted Elements In List
I have a list ['Roll_Number','Marks in subjects']: list_1=[(1,22),(1,22),(2,29),(2,16),(3,56),(4,32),(4,12)] I tried this code in python to get sum of all subjects for each roll_n
Solution 1:
You can do that as follows:
from collections import defaultdict
a = [(1,22),(1,22),(2,29),(2,16),(3,56),(4,32),(4,12)]
b = defaultdict(lambda: 0)
for i,j in a:
b[i] += j
>>> print b
{1:44, 2:45, 3:56, 4:44}
DEMO
Solution 2:
Since your list is sorted by first element already, you can use itertools.groupby
for a relatively simple one-line solution:
>>>from itertools import groupby as gb>>>from operator import itemgetter>>>>>>l = [(1,22),(1,22),(2,29),(2,16),(3,56),(4,32),(4,12)]>>>>>>{a: sum(t[1] for t in b) for a,b in gb(l, itemgetter(0))}
{1: 44, 2: 45, 3: 56, 4: 44}
Solution 3:
Using defaultdict
>>>list_1=[(1,22),(1,22),(2,29),(2,16),(3,56),(4,32),(4,12)]>>>from collections import defaultdict>>>res = defaultdict(int)>>>for key, val in list_1:... res[key] += val...>>>print res
defaultdict(<type 'int'>, {1: 44, 2: 45, 3: 56, 4: 44})
>>>printdict(res)
{1: 44, 2: 45, 3: 56, 4: 44}
Solution 4:
you can also use the groupby
from itertools
>>>from itertools import groupby>>>from operator import itemgetter>>>result = [(k, sum(r[1] for r in rows)) for k, rows in groupby(list_1, key=itemgetter(0))]>>>result
[(1, 44), (2, 45), (3, 56), (4, 44)]
Solution 5:
Not the most intuitive
from collections import Counter
from itertools import chain, repeat
Counter(chain.from_iterable(repeat(x, y) for x, y in list_1))
Counter({3: 56, 2: 45, 1: 44, 4: 44})
Post a Comment for "Adding The Sorted Elements In List"