简体   繁体   中英

Python - Summing elements on list of lists based on first element of inner lists

I have a list

[[0.5, 2], [0.5, 5], [2, 3], [2, 6], [2, 0.6], [7, 1]]

I require the output from summing the second element in each sublist for a unique first element. In this case it is:

[[0.5, 7], [2, 9.6], [7, 1]]

Where [0.5, 7] is summing the second element of [0.5, 2] and [0.5, 5] .

What would be the most efficient way to do this for say lists of length 1,000.

Accumulate with a defaultdict :

>>> from collections import defaultdict
>>> data = defaultdict(int)
>>> L = [[0.5, 2], [0.5, 5], [2, 3], [2, 6], [2, 0.6], [7, 1]]
>>> for k, v in L:
...     data[k] += v
...     
>>> [[k,v] for (k,v) in data.items()]
[[0.5, 7], [2, 9.6], [7, 1]]

Note that the value for 2 was automatically "promoted" to a float by addition, even though this is a defaultdict of int. This is to match the desired output posted in the question, but I think you should consider to use homogeneous output types rather than a mix of int and float.

Using Pandas, you can retain the original 'order' of the data:

pairs = [[0.5, 2], [0.5, 5], [2, 3], [2, 6], [2, 0.6], [7, 1]]
df = pd.DataFrame(pairs)
>>> [tup[0] for tup in zip(df.groupby(0, sort=False, as_index=False).sum().values.tolist())]
[[0.5, 7.0], [2.0, 9.6], [7.0, 1.0]]

You can get away with sorting and itertools.groupby :

from operator import itemgetter
from itertools import groupby

data = [[0.5, 2], [0.5, 5], [2, 3], [2, 6], [2, 0.6], [7, 1]]

key = itemgetter(0)
data.sort(key=key)  # Use data = sorted(data, key=key) to avoid clobbering
result = [[k, sum(group)] for k, group in groupby(data, key)]

This will not preserve the original order of the keys.

Will this work?

L = [[0.5, 2], [0.5, 5], [2, 3], [2, 6], [2, 0.6], [7, 1]]
nums = []
d = {}
for lst in L:
    if lst[0] not in d:
        d[lst[0]] = []
        nums.append(lst[0])
    d[lst[0]].append(lst[1])

for key in nums:
    print [key, sum(d[key])]

Output:

[0.5, 7]
[2, 9.6]
[7, 1]

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