构建一个字符串来评估它不是很优雅。更好地与 ,reduce
结合使用,所有这些都由 list和 dict comprehensionsnp.add
启用。首先,将字典列表转换为列表字典:[]
{}
R
S
S = {k:[ R[j][k] for j in range(len(R)) ] for k in R[0].keys()}
现在,每个键只有一个“裸”numpy数组列表,可以使用添加np.add
然后除以单个列表的长度:
S = {'a1': [array([[ 32, 120, 80, 380, 360, 212, 188, 56, 312, 112],
[388, 348, 196, 236, 60, 200, 224, 208, 24, 104],
[324, 296, 24, 52, 220, 12, 104, 52, 232, 196]]),
array([[ 32, 120, 80, 380, 360, 212, 188, 56, 312, 112],
[388, 348, 196, 236, 60, 200, 224, 208, 24, 104],
[324, 296, 24, 152, 220, 12, 104, 52, 232, 196]])],
'a2': [array([[30, 82, 99, 72, 79, 98, 93, 93, 28, 46],
[ 8, 17, 50, 59, 85, 73, 48, 97, 87, 41],
[98, 36, 27, 55, 98, 39, 73, 51, 27, 33]]),
array([[30, 82, 99, 72, 79, 98, 93, 93, 28, 46],
[ 8, 17, 50, 59, 85, 73, 48, 97, 87, 41],
[98, 36, 27, 55, 98, 39, 73, 51, 27, 33]])],
'a3': [array([[78, 24, 87, 83, 30, 14, 88, 57, 55, 73],
[76, 94, 99, 58, 63, 34, 70, 81, 45, 20],
[32, 61, 0, 3, 33, 33, 38, 90, 11, 3]]),
array([[78, 24, 87, 83, 30, 14, 88, 57, 55, 73],
[76, 94, 99, 58, 63, 34, 70, 81, 45, 20],
[32, 61, 0, 3, 33, 33, 38, 90, 11, 3]])]}
计算平均值:
T = {k:( reduce(np.add, v)/len(v) ) for k,v in S.iteritems()}
现在T
是具有平均值的 numpy 数组的字典:
T = {'a1': array([[ 32, 120, 80, 380, 360, 212, 188, 56, 312, 112],
[388, 348, 196, 236, 60, 200, 224, 208, 24, 104],
[324, 296, 24, 152, 220, 12, 104, 52, 232, 196]]),
'a2': array([[30, 82, 99, 72, 79, 98, 93, 93, 28, 46],
[ 8, 17, 50, 59, 85, 73, 48, 97, 87, 41],
[98, 36, 27, 55, 98, 39, 73, 51, 27, 33]]),
'a3': array([[78, 24, 87, 83, 30, 14, 88, 57, 55, 73],
[76, 94, 99, 58, 63, 34, 70, 81, 45, 20],
[32, 61, 0, 3, 33, 33, 38, 90, 11, 3]])}