|
发表于 2023-1-6 16:09:50
|
显示全部楼层
本帖最后由 isdkz 于 2023-1-6 16:12 编辑
>>> help(sum)
Help on built-in function sum in module builtins:
sum(iterable, /, start=0)
Return the sum of a 'start' value (default: 0) plus an iterable of numbers
When the iterable is empty, return the start value.
This function is intended specifically for use with numeric values and may
reject non-numeric types.
>>>
查看 sum 的帮助文档可以看到第二个参数为求和的初始值
比如:
- a = ([1], [2], [3])
- a1 = sum(a, [])
- # 就相当于
- a1 = [] + [1] + [2] + [3]
- a2 = sum(a, [4])
- # 就相当于
- a2 = [4] + [1] + [2 ] + [3] # 结果为 [4, 1, 2, 3]
复制代码
如果你的 a 中的元素不是数字的话就必须设置初始值,因为默认的初始值为 0,
数字 0 是不能和非数字类型的相加的
|
|