假设我有一个类 C,它具有属性 a.
Lets say I have class C which has attribute a.
从 Python 中的 C 列表中获取 a 总和的最佳方法是什么?
What is the best way to get the sum of a from a list of C in Python?
我已经尝试了以下代码,但我知道这不是正确的做法:
I've tried the following code, but I know that's not the right way to do it:
for c in c_list:
total += c.a
使用 生成器表达式:
sum(c.a for c in c_list)
这篇关于对对象列表的属性求和/平均的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!
列表字典中的总和值Sum values in a dict of lists(列表字典中的总和值)
如何在 Python 中对文本文件中的数字求和How to sum numbers from a text file in Python(如何在 Python 中对文本文件中的数字求和)
什么是类似于 sum() 的减法函数,用于减去列表中What is a subtraction function that is similar to sum() for subtracting items in list?(什么是类似于 sum() 的减法函数,用于减去列表中的
Python 等价于 sum() 使用 xor()Python equivalent of sum() using xor()(Python 等价于 sum() 使用 xor())
python中的求和矩阵列sum matrix columns in python(python中的求和矩阵列)
N个列表元素的总和pythonsum of N lists element-wise python(N个列表元素的总和python)