简体   繁体   English

数组中第二个元素的总和

[英]Sum of second element in an array

List Format: [['CODE','ACCURACY'],['CODE','ACCURACY'],....]列表格式:[['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:示例目标 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]

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:这是一种构造预期均值字典的方法,使用defaultdict收集每个键的数字,然后使用statistics模块来计算均值:

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()}

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)

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)

The output result could be written as a dictionary comprehension directly from the input list: output 结果可以直接从输入列表写成字典推导:

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.然后,使用每个代码作为过滤器参数( uniquecode ),我们从该唯一代码计算准确度的平均值,并以uniquecode作为字典键和计算的每个代码的平均准确度作为值来构建字典。

The output should be: output 应该是:

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

Probably @john-coleman's response is easier to read, and this is more compact.可能@john-coleman 的回复更容易阅读,而且更紧凑。 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.您也可以 output 一个元组列表,但我认为字典对于这种情况更有意义。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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