Mike Zhang
Mike Zhang

Reputation: 45

Sum of second element in an array

List Format: [['CODE','ACCURACY'],['CODE','ACCURACY'],....]

Example List:

list1 = [['L5TPPPD9KZST', 0.9707996249198914], ['L5TPPPD9KZST', 0.9604377150535583], ['L5TPPPD9KZST', 0.9624994397163391], ['L5TPPPD9KZST', 0.955596387386322], ['L5TPPPD9KZST', 0.9619339108467102], ['L5TPPPD9KZST', 0.9616058468818665], ['L5TPPPD9KZST', 0.9630076289176941], ['L5TPPPD9KZST', 0.9631552696228027], ['L5TPPPD9KZST', 0.965229332447052], ['L5TPPPD9KZST', 0.965229332447052], ['L5TPPPD9KZST', 0.9654726982116699], ['L5TPPPD9KZST', 0.964877188205719], ['L5TPPPD9KZST', 0.9704579710960388], ['L5TPPPD9KZST', 0.970452606678009], ['L5TPPPD9KZST', 0.9722679257392883], ['L5TPPPD9KZST', 0.969722330570221], ['L5TPPPD9KZST', 0.9694883823394775], ['L5TPPPD9KZST', 0.9694886803627014], ['L5TPPPD9KZST', 0.9639778137207031], ['L5TPPPD9KZST', 0.9639263153076172], ['L5TPPPD9KZST', 0.9583439230918884], ['L5TPPPD9KZST', 0.9393630623817444], ['L5TPPPD9KZST', 0.9582712054252625], ['L5TPPPD9KZST', 0.9605154395103455], ['L5TPPPD9KZST', 0.961374819278717], ['L5TPPPD9KZST', 0.9609169960021973], ['L5TPPPD9KZST', 0.9613582491874695], ['L5TPPPD9KZST', 0.9693458080291748], ['L5TPPPD9KZST', 0.9687929749488831], ['L5TPPPD9KZST', 0.9689026474952698], ['L5TPPPD9KZST', 0.9687891602516174], ['L5TPPPD9KZST', 0.9687891602516174], ['L5TPPPD9KZST', 0.96875], ['L5TPPPD9KZST', 0.9687290787696838], ['L5TPPPD9KZST', 0.968794047832489], ['L5TPPPD9KZST', 0.9696788787841797], ['L5TPPPD9KZST', 0.9701188206672668], ['L5TPPPD9KZST', 0.97011399269104], ['L5TPPPD9KZST', 0.9701710343360901], ['L5TPPPD9KZST', 0.9710682034492493], ['L5TPPPD9KZST', 0.9711113572120667], ['L5TPPPD9KZST', 0.971106231212616], ['L5TPPPD9KZST', 0.9664766192436218], ['L5TPPPD9KZST', 0.9712474346160889], ['L5TPPPD9KZST', 0.9665161967277527], ['L5TPPPD9KZST', 0.9663145542144775], ['L5TPPPD9KZST',
0.9663145542144775], ['L5TPPPD9KZST', 0.9660029411315918], ['L5TPPPD9KZST', 0.9660454392433167], ['L5TPPPD9KZST', 0.9657848477363586], ['L5TPPPD9KZST', 0.9658169746398926], ['L5TPPPD9KZST', 0.9657814502716064], ['L5TPPPD9KZST', 0.96523118019104], ['L5TPPPD9KZST', 0.9655305743217468], ['L5TPPPD9KZST', 0.9731234908103943], ['L5TPPPD9KZST', 0.9739508032798767], ['L5TPPPD9KZST', 0.9743876457214355], ['L5TPPPD9KZST', 0.9743876457214355], ['L5TPPPD9KZST', 0.9726870656013489], ['L5TPPPD9KZST', 0.9724721908569336], ['L5TPPPD9KZST', 0.9730187058448792], ['L5TPPPD9KZST', 0.9730861186981201], ['L5TPPPD9KZST', 0.9731133580207825], ['L5TPPPD9KZST', 0.9731297492980957], ['L5TPPPD9KZST', 0.9568511843681335], ['L5TPPPD9KZST', 0.9568330645561218], ['L5TPPPD9KZST', 0.9570344090461731], ['L5TPPPD9KZST', 0.9725109934806824], ['L5TPPPD9KZST', 0.9727892875671387], ['L5TPPPD9KZST',
0.9726458191871643], ['L5TPPPD9KZST', 0.9727286696434021], ['L5TPPPD9KZST', 0.9725451469421387], ['L5TPPPD9KZST', 0.9716582298278809], ['L5TPPPD9KZST', 0.9698121547698975], ['L5TPPPD9KZST', 0.96990567445755], ['L5TPPPD9KZST', 0.9672815799713135], ['L5TPPPD9KZST', 0.9675748944282532], ['L5TPPPD9KZST', 0.9671061038970947], ['L5TPPPD9KZST', 0.9657451510429382], ['L5TPPPD9KZST', 0.9660928845405579], ['L5TPPPD9KZST', 0.9660732746124268], ['L5TPPPD9KZST', 0.9660385251045227], ['L5TPPPD9KZST', 0.966016948223114], ['L5TPPPD9KZST', 0.9660078883171082], ['L5TPPPD9KZST', 0.9658970832824707], ['L5TPPPD9KZST', 0.9659187197685242], ['L5TPPPD9KZST', 0.9659130573272705], ['L5TPPPD9KZST', 0.9659150242805481], ['L5TPPPD9KZST', 0.9658761024475098], ['L5TPPPD9KZST', 0.964733362197876], ['L5TPPPD9KZST', 0.9646833539009094], ['L5TPPPD9KZST', 0.9646801352500916], ['L5TPPPD9KZST', 0.9646833539009094], ['L5TPPPD9KZST', 0.9646837115287781], ['L5TPPPD9KZST', 0.9643750190734863], ['L5TPPPD9KZST', 0.9643678069114685], ['L5TPPPD9KZST', 0.9624671339988708], ['L5TPPPD9KZST', 0.9624366164207458], ['L5TPPPD9KZST', 0.9487274289131165], ['L5TPPPO9KZST', 0.9608916640281677], ['L5TPPPD9KZST', 0.9646546840667725], ['L5TPPPD9KZST', 0.970625102519989], ['L5TPPP09KZST', 0.947458028793335], ['L5TPPP09KZST', 0.940028727054596], ['L5TPPP09KZST', 0.940028727054596], ['L5TPPP09KZST', 0.9396738409996033], ['L5TPPPO9KZST', 0.9395968317985535], ['L5TPPPO9KZST', 0.9395968317985535], ['L5TPPPO9KZST', 0.939121425151825], ['L5TPPPO9KZST', 0.940077543258667], ['L5TPPPO9KZST', 0.9393913745880127], ['L5TPPPO9KZST', 0.9392940402030945], ['L5TPPPO9KZST', 0.9404256939888], ['L5TPPPO9KZST', 0.9413366317749023], ['L5TPPPO9KZST', 0.9441054463386536], ['L5TPPPO9KZST', 0.9450051188468933], ['L5TPPPO9KZST', 0.9424901604652405], ['L5TPPPO9KZST', 0.941277801990509], ['L5TPPPO9KZST', 0.9412491321563721], ['L5TPPPO9KZST', 0.9403512477874756], ['L5TPPPO9KZST', 0.9425294399261475], ['L5TPPPO9KZST',
0.9383044242858887], ['L5TPPPO9KZST', 0.9386259913444519], ['L5TPPPO9KZST', 0.9404183030128479], ['L5TPPPO9KZST', 0.9435202479362488], ['L5TPPPO9KZST', 0.9464590549468994], ['L5TPPPO9KZST', 0.9463607668876648]]

Goal:

How can we get the average ACCURACY of one CODE in the most effient way?

Example Goal Output:

['L5TPPPO9KZST','AVERAGE ACCURACY']

This is so far I've archived:

d1 = list(set([item[0] for item in list1]))
print(d1)

for codeSingle in d1:
    print(codeSingle)
    for scores in list1:
        if codeSingle in scores[0]:
            score = scores[1]

Upvotes: 0

Views: 173

Answers (4)

Carlos Melus
Carlos Melus

Reputation: 1552

The output result could be written as a dictionary comprehension directly from the input list:

import statistics

means = {uniquecode: statistics.mean([accuracy for code, accuracy in list1 if code == uniquecode]) for uniquecode in {c for c, a in list1}}

First, the unique codes in the list are extracted using a set comprehension:

{c for c, a in list1}

Then, using each code as the filter parameter (uniquecode), we calculate the mean of the accuracies from that unique code, and build the dictionary with uniquecode as the dictionary keys and the calculated average accuracy for each code as the values.

The output should be:

{'L5TPPPO9KZST': 0.9422922351143577,
 'L5TPPPD9KZST': 0.9665182238758201,
 'L5TPPP09KZST': 0.9417973309755325}

Probably @john-coleman's response is easier to read, and this is more compact. With a list this size there is little performance difference between both solutions. It depends on what efficient means to you.

You could also output a list of tuples, but I think a dictionary makes more sense for this scenario.

Upvotes: 0

Jacob
Jacob

Reputation: 820

One option is to sum and keep track of the counts of each key in a dictionary and then calculate the means of each after

d = {}

for key, value in list1:
    try:
        d[key][0] += value
        d[key][1] += 1
    except KeyError:
        d[key] = [value, 1]

d = {key: sum_count[0] / sum_count[1] for key, sum_count in d.items()}

print(d)

Upvotes: 0

kindall
kindall

Reputation: 184375

Given a code name, the following will first create a new list with all the scores for that code, then calculate the average of those scores.

def average_code_score(code, scores):
    scores_for_code = [x[1] for x in scores if x[0] == code]
    return sum(scores_for_code) / len(scores_for_code)

Upvotes: 0

John Coleman
John Coleman

Reputation: 52008

Here is one approach which constructs a dictionary of the intended means, using a defaultdict to gather the numbers for each key and then using the statistics module to compute the means:

import collections, statistics

list1 = [['L5TPPPD9KZST', 0.9707996249198914], ['L5TPPPD9KZST', 0.9604377150535583], ['L5TPPPD9KZST', 0.9624994397163391], ['L5TPPPD9KZST', 0.955596387386322], ['L5TPPPD9KZST', 0.9619339108467102], ['L5TPPPD9KZST', 0.9616058468818665], ['L5TPPPD9KZST', 0.9630076289176941], ['L5TPPPD9KZST', 0.9631552696228027], ['L5TPPPD9KZST', 0.965229332447052], ['L5TPPPD9KZST', 0.965229332447052], ['L5TPPPD9KZST', 0.9654726982116699], ['L5TPPPD9KZST', 0.964877188205719], ['L5TPPPD9KZST', 0.9704579710960388], ['L5TPPPD9KZST', 0.970452606678009], ['L5TPPPD9KZST', 0.9722679257392883], ['L5TPPPD9KZST', 0.969722330570221], ['L5TPPPD9KZST', 0.9694883823394775], ['L5TPPPD9KZST', 0.9694886803627014], ['L5TPPPD9KZST', 0.9639778137207031], ['L5TPPPD9KZST', 0.9639263153076172], ['L5TPPPD9KZST', 0.9583439230918884], ['L5TPPPD9KZST', 0.9393630623817444], ['L5TPPPD9KZST', 0.9582712054252625], ['L5TPPPD9KZST', 0.9605154395103455], ['L5TPPPD9KZST', 0.961374819278717], ['L5TPPPD9KZST', 0.9609169960021973], ['L5TPPPD9KZST', 0.9613582491874695], ['L5TPPPD9KZST', 0.9693458080291748], ['L5TPPPD9KZST', 0.9687929749488831], ['L5TPPPD9KZST', 0.9689026474952698], ['L5TPPPD9KZST', 0.9687891602516174], ['L5TPPPD9KZST', 0.9687891602516174], ['L5TPPPD9KZST', 0.96875], ['L5TPPPD9KZST', 0.9687290787696838], ['L5TPPPD9KZST', 0.968794047832489], ['L5TPPPD9KZST', 0.9696788787841797], ['L5TPPPD9KZST', 0.9701188206672668], ['L5TPPPD9KZST', 0.97011399269104], ['L5TPPPD9KZST', 0.9701710343360901], ['L5TPPPD9KZST', 0.9710682034492493], ['L5TPPPD9KZST', 0.9711113572120667], ['L5TPPPD9KZST', 0.971106231212616], ['L5TPPPD9KZST', 0.9664766192436218], ['L5TPPPD9KZST', 0.9712474346160889], ['L5TPPPD9KZST', 0.9665161967277527], ['L5TPPPD9KZST', 0.9663145542144775], ['L5TPPPD9KZST',
0.9663145542144775], ['L5TPPPD9KZST', 0.9660029411315918], ['L5TPPPD9KZST', 0.9660454392433167], ['L5TPPPD9KZST', 0.9657848477363586], ['L5TPPPD9KZST', 0.9658169746398926], ['L5TPPPD9KZST', 0.9657814502716064], ['L5TPPPD9KZST', 0.96523118019104], ['L5TPPPD9KZST', 0.9655305743217468], ['L5TPPPD9KZST', 0.9731234908103943], ['L5TPPPD9KZST', 0.9739508032798767], ['L5TPPPD9KZST', 0.9743876457214355], ['L5TPPPD9KZST', 0.9743876457214355], ['L5TPPPD9KZST', 0.9726870656013489], ['L5TPPPD9KZST', 0.9724721908569336], ['L5TPPPD9KZST', 0.9730187058448792], ['L5TPPPD9KZST', 0.9730861186981201], ['L5TPPPD9KZST', 0.9731133580207825], ['L5TPPPD9KZST', 0.9731297492980957], ['L5TPPPD9KZST', 0.9568511843681335], ['L5TPPPD9KZST', 0.9568330645561218], ['L5TPPPD9KZST', 0.9570344090461731], ['L5TPPPD9KZST', 0.9725109934806824], ['L5TPPPD9KZST', 0.9727892875671387], ['L5TPPPD9KZST',
0.9726458191871643], ['L5TPPPD9KZST', 0.9727286696434021], ['L5TPPPD9KZST', 0.9725451469421387], ['L5TPPPD9KZST', 0.9716582298278809], ['L5TPPPD9KZST', 0.9698121547698975], ['L5TPPPD9KZST', 0.96990567445755], ['L5TPPPD9KZST', 0.9672815799713135], ['L5TPPPD9KZST', 0.9675748944282532], ['L5TPPPD9KZST', 0.9671061038970947], ['L5TPPPD9KZST', 0.9657451510429382], ['L5TPPPD9KZST', 0.9660928845405579], ['L5TPPPD9KZST', 0.9660732746124268], ['L5TPPPD9KZST', 0.9660385251045227], ['L5TPPPD9KZST', 0.966016948223114], ['L5TPPPD9KZST', 0.9660078883171082], ['L5TPPPD9KZST', 0.9658970832824707], ['L5TPPPD9KZST', 0.9659187197685242], ['L5TPPPD9KZST', 0.9659130573272705], ['L5TPPPD9KZST', 0.9659150242805481], ['L5TPPPD9KZST', 0.9658761024475098], ['L5TPPPD9KZST', 0.964733362197876], ['L5TPPPD9KZST', 0.9646833539009094], ['L5TPPPD9KZST', 0.9646801352500916], ['L5TPPPD9KZST', 0.9646833539009094], ['L5TPPPD9KZST', 0.9646837115287781], ['L5TPPPD9KZST', 0.9643750190734863], ['L5TPPPD9KZST', 0.9643678069114685], ['L5TPPPD9KZST', 0.9624671339988708], ['L5TPPPD9KZST', 0.9624366164207458], ['L5TPPPD9KZST', 0.9487274289131165], ['L5TPPPO9KZST', 0.9608916640281677], ['L5TPPPD9KZST', 0.9646546840667725], ['L5TPPPD9KZST', 0.970625102519989], ['L5TPPP09KZST', 0.947458028793335], ['L5TPPP09KZST', 0.940028727054596], ['L5TPPP09KZST', 0.940028727054596], ['L5TPPP09KZST', 0.9396738409996033], ['L5TPPPO9KZST', 0.9395968317985535], ['L5TPPPO9KZST', 0.9395968317985535], ['L5TPPPO9KZST', 0.939121425151825], ['L5TPPPO9KZST', 0.940077543258667], ['L5TPPPO9KZST', 0.9393913745880127], ['L5TPPPO9KZST', 0.9392940402030945], ['L5TPPPO9KZST', 0.9404256939888], ['L5TPPPO9KZST', 0.9413366317749023], ['L5TPPPO9KZST', 0.9441054463386536], ['L5TPPPO9KZST', 0.9450051188468933], ['L5TPPPO9KZST', 0.9424901604652405], ['L5TPPPO9KZST', 0.941277801990509], ['L5TPPPO9KZST', 0.9412491321563721], ['L5TPPPO9KZST', 0.9403512477874756], ['L5TPPPO9KZST', 0.9425294399261475], ['L5TPPPO9KZST',
0.9383044242858887], ['L5TPPPO9KZST', 0.9386259913444519], ['L5TPPPO9KZST', 0.9404183030128479], ['L5TPPPO9KZST', 0.9435202479362488], ['L5TPPPO9KZST', 0.9464590549468994], ['L5TPPPO9KZST', 0.9463607668876648]]

d = collections.defaultdict(list)
for k,v in list1: d[k].append(v)
means = {k:statistics.mean(v) for k,v in d.items()}

Upvotes: 2

Related Questions