在嵌套列表中添加元素并输出列表python

可汗

我想添加

distances
[array([2.0786226e-03, 3.9023757e-03, 3.4480095e-03, 5.1999092e-04], dtype=float32), array([0.0031136 , 0.00344056, 0.00739563, 0.0311079 ],
      dtype=float32), array([1.8880665e-03, 3.0295253e-03, 4.0802956e-03, 2.6324868e-02], dtype=float32), array([0.00330418, 0.00431347, 0.00802791, 0.00426304],
      dtype=float32)]

期望输出

[0.00994899872, 0.04505769, 0.0353227554, 0.0199086]

我尝试了以下但它添加了所有元素并给出了标量值

print(sum( sum(x) if isinstance(x, list) else x for x in L ))  
斯科特·亨特

假设Ldistances,你几乎就在那里:

print([sum(x) if isinstance(x, list) else x for x in L ])  

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章