If there is:
random_list = [OrderedDict([('num1',1.0), ('num2', 2.0), ('num3', 3.0)]),OrderedDict([('num1',10.0), ('num2', 20.0), ('num3', 30.0])]
In none of the above, sum() should be used.
Thanks.
You work with OrderedDict like with standard Python's dict:
from collections import OrderedDict
random_list = [OrderedDict([('num1',1.0), ('num2', 2.0), ('num3', 3.0)]),OrderedDict([('num1',10.0), ('num2', 20.0), ('num3', 30.0)])]
case1 = sum(d['num2'] for d in random_list)
print(case1)
case2 = sum(d['num2'] + d['num3'] for d in random_list)
print(case2)
Prints:
22.0
55.0
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.