简体   繁体   English

汇总字典中的特定项目

[英]Sum specific items from dictionary

I have a dictionary named dict with 50 items but -unfortunately- names do not have a specific pattern. 我有一本名为dict其中有50项内容,但-不幸的是,名称没有特定的模式。

I want to get the sum of specific variables and do some checks: 我想获取特定变量的总和并进行一些检查:

dict[random_name_sum] == dict['random_name_1'] + dict['random_name_2'] + dict['random_name_3']

but the following error appears: 但是出现以下错误:

TypeError: unsupported operand type(s) for +: 'int' and 'list'

I searched for similar questions, but none of them can solve my problem, and the only solution I can think of is changing the data structure that may require some future manual actuons. 我搜索了类似的问题,但没有一个能够解决我的问题,我能想到的唯一解决方案是更改可能需要将来进行一些手动操作的数据结构。 I would like to avoid that. 我想避免这种情况。 Do you have any ideas? 你有什么想法?

首先检查dict值的返回类型是否为int,因为好像您有一个列表。

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

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