英文:
TypeError when sum value of dictionnary for each key
问题
I want to sum one value that contains list inside value dictionary.
我想要对包含列表的字典值进行求和。
I want to sum value of key x1
in dataX
dictionary that contains list ([12, 14, 10, 16, 18, 24, 12]
) but it goes wrong and an error appears.
我想要对包含列表 ([12, 14, 10, 16, 18, 24, 12]
) 的 dataX
字典中的键 x1
的值进行求和,但出现了错误。
Here is my dictionary:
这是我的字典:
dataX = {'x1': [12, 14, 10, 16, 18, 24, 12], 'x2': [10, 11, 14, 13, 15, 20, 8]}
字典如下:
dataX = {'x1': [12, 14, 10, 16, 18, 24, 12], 'x2': [10, 11, 14, 13, 15, 20, 8]}
the output should :
输出应为:
106
应该输出:
106
Here is my code:
这是我的代码:
sum = 0
for i in dataX:
sum += dataX[i]
但是,我遇到了一个错误:
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
/var/folders/s6/_5szgcrd2cl86md1wj_kw_th0000gp/T/ipykernel_18467/658532054.py in <module>
9 sum = 0
10 for i in dataX:
---> 11 sum += dataX[i]
TypeError: unsupported operand type(s) for +=: 'int' and 'list'
但是,我遇到了一个错误:
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
/var/folders/s6/_5szgcrd2cl86md1wj_kw_th0000gp/T/ipykernel_18467/658532054.py in <module>
9 sum = 0
10 for i in dataX:
---> 11 sum += dataX[i]
TypeError: unsupported operand type(s) for +=: 'int' and 'list'
英文:
I want to sum one value that contains list inside value dictionary.
I want to sum value of key 'x1'
in dataX
dictionary that contains list ([12, 14, 10, 16, 18, 24, 12]
) but it goes wrong and an error appears.
Here is my dictionary:
dataX = {'x1' : [12, 14, 10, 16, 18, 24, 12], 'x2' : [10, 11, 14, 13, 15, 20, 8]}
the output should :
106
Here is my code:
sum = 0
for i in dataX:
sum += dataX[i]
But, I get an error:
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
/var/folders/s6/_5szgcrd2cl86md1wj_kw_th0000gp/T/ipykernel_18467/658532054.py in <module>
9 sum = 0
10 for i in dataX:
---> 11 sum += dataX[i]
TypeError: unsupported operand type(s) for +=: 'int' and 'list'
答案1
得分: 1
使用内置的sum()函数
您需要使用内置的sum函数,但要小心,您正在对变量sum
进行名称遮蔽。
在编程中,遮蔽是指在内部作用域(您的文件)中重用相同的变量或函数名称(这里是sum
),从而隐藏了其在外部作用域(内置函数)中的定义。这可能导致外部定义在一段时间内无法访问(您无法在文件中使用sum
),因为内部定义具有优先权(您的sum
变量是一个整数)。
> ⚠️ 如果您正在使用Jupyter Notebook或交互式会话,请注意,即使您在代码中更改了变量,也需要重新启动Python会话,因为sum
在会话的内存框架中已更改。您需要重新启动Python来恢复sum
函数。
如果您想要每个值的总和:
sum_dict = {}
for key in dataX:
sum_dict[key] = sum(dataX[key])
print(sum_dict)
# {'x1': 106, 'x2': 91}
一些改进
遍历键值对
dict.items()
允许您遍历字典的键值对,这样您可以一起操作键和值,减少了数据处理的样板代码。
sum_dict = {}
for key, values in dataX.items():
sum_dict[key] = sum(values)
print(sum_dict)
# {'x1': 106, 'x2': 91}
使用字典推导
如果您想要一个包含每个列表键和总和的字典,那么使用字典推导是合适的,因为这个操作很简单,可以这样表达而不降低可读性。
sum_dict = {key: sum(values) for key, values in dataX.items()}
print(sum_dict)
# {'x1': 106, 'x2': 91}
英文:
Use the built-in sum() function
You need to use the sum built-in function, but be careful, you are doing name shadowing with the variable sum
.
In programming, shadowing refers to the act of reusing the same name for a variable or function (here, sum
) in an inner scope (your file), hiding its definition in an outer scope (built-ins). This can result in the outer definition being temporarily inaccessible (you can't use sum
in your file), because the inner definition takes precedence (your sum
variable, an int).
> ⚠️ If you are using Jupyter Notebook or an interactive
> session, please note that you need to restart the python session
> even if you changed the variable in the code, because sum
has
> changed in the memory frame of the session. You need to restart Python
> to restore the sum
function.
If you want the sum of each values:
sum_dict = {}
for key in dataX:
sum_dict[key] = sum(dataX[key])
print(sum_dict)
# {'x1': 106, 'x2': 91}
Some improvements
iterate over key-value pairs
dict.items()
allows you to iterate over the dictionnary with pairs of key and value together, which make manipulation of data less boilerplate.
sum_dict = {}
for key, values in dataX.items():
sum_dict[key] = sum(values)
print(sum_dict)
# {'x1': 106, 'x2': 91}
Use dict comprehension
If you want a dict with keys and total for each list, a dict comprehension is fine since this operation is simple and can be expressed like this without reducing readability.
sum_dict = {key: sum(values) for key, values in dataX.items()}
print(sum_dict)
# {'x1': 106, 'x2': 91}
答案2
得分: 1
There is one more issue in your code:
你的代码中还有一个问题;
You are using the builtin sum
as your variable name.
你使用了内置函数 sum
作为你的变量名。
It will override the builtin function and will not add values. sum
will behave as an integer.
它会覆盖内置函数,并不会添加值。sum
会被视为整数。
It is strongly advised not to use builtin function names like list
, sum
, etc. as your variable names.
强烈建议不要将内置函数名称如 list
、sum
等用作你的变量名。
you can get the sum of value by:
你可以通过以下方法得到值的总和:
dataX = {'x1': [12, 14, 10, 16, 18, 24, 12], 'x2': [10, 11, 14, 13, 15, 20, 8]}
sum(dataX['x1'])
#106
或者
for i in dataX:
print(sum(dataX[i]))
106
91
英文:
There is one more issue in your code;
You are using the builtin sum
as your variable name.
It will override the builtin function and will not add values. sum
will behave as an integer.
It is strongly advised not to use builtin function names like list
, sum
, etc. as your variable names.
you can get the sum of value by:
dataX = {'x1' : [12, 14, 10, 16, 18, 24, 12], 'x2' : [10, 11, 14, 13, 15, 20, 8]}
sum(dataX['x1'])
#106
or
for i in dataX:
print(sum(dataX[i]))
106
91
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论