1、首先我们看sum函数
def sum(*args, **kwargs): # real signature unknown
"""
Return the sum of a 'start' value (default: 0) plus an iterable of numbers
返回start的值(默认为0)加上一个数字组成的可迭代对象的和。
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.
"""
pass
2、常规用法如,求和
print(sum([1,2], 5))
3、也可以用来将多维列表,元组,字典里的元素连接成一维的。
如下,
print(sum([[1, 2],[3, 4, 5]], []))
结果输出如下
[1, 2, 3, 4, 5]
元组也是如此,例如:
def shuffle(nums, n):
return sum(zip(nums[:n], nums[n:]),())
print(shuffle([1,2,3,4,4,3,2,1],4))
输出如下
(1, 4, 2, 3, 3, 2, 4, 1)