python - Rapid compression of multiple lists with value addition -


i looking pythonic way iterate through large number of lists , use index of repeated values 1 list calculate total value values same index in list.

for example, have 2 lists

a = [ 1, 2, 3, 1, 2, 3, 1, 2, 3] b = [ 1, 2, 3, 4, 5, 6, 7, 8, 9] 

what want find unique values in a, , add corresponding values b same index. attempt, quite slow, follows:

a1=list(set(a)) b1=[0 y in range(len(a1))]      m in range(len(a)):         k in range(len(a1)):             if a1[k]==a[m]:                 b1[k]+=b[m] 

and get

a1=[1, 2, 3] b1=[12, 15, 18] 

please let me know if there faster, more pythonic way this. thanks

use zip() function , defaultdict dictionary collect values per unique value:

from collections import defaultdict try:     # python 2 compatibility     future_builtins import zip except importerror:     # python 3, there     pass  values = defaultdict(int) key, value in zip(a, b):     values[key] += value  a1, b1 = zip(*sorted(values.items())) 

zip() pairs values 2 input lists, have sum each value b per unique value of a.

the last line pulls out keys , values resulting dictionary, sorts these, , puts keys , values a1 , b1, respectively.

demo:

>>> collections import defaultdict >>> = [ 1, 2, 3, 1, 2, 3, 1, 2, 3] >>> b = [ 1, 2, 3, 4, 5, 6, 7, 8, 9] >>> values = defaultdict(int) >>> key, value in zip(a, b): ...     values[key] += value ... >>> zip(*sorted(values.items())) [(1, 2, 3), (12, 15, 18)] 

if don't care output order, can drop sorted() call altogether.


Comments

Popular posts from this blog

sequelize.js - Sequelize group by with association includes id -

android - Robolectric "INTERNET permission is required" -

java - Android raising EPERM (Operation not permitted) when attempting to send UDP packet after network connection -