Manual
Sums start and the items of an iterable from left to right and returns the total. start defaults to 0. The iterable‘s items are normally numbers, and the start value is not allowed to be a string.
For some use cases, there are good alternatives to sum(). The preferred, fast way to concatenate a sequence of strings is by calling ”.join(sequence). To add floating point values with extended precision, see math.fsum(). To concatenate a series of iterables, consider using itertools.chain().
直译
计算start和iterable从左到右的项合计。
start默认为0,其值不允许字符串;iterable的项通常是数字。
对于一些其他使用情形,这有一些更好的替代sum()的选择。首选,可以通过调用”.join(sequence)快速链接字符串序列;用扩展精度运算math.fsum()求浮点值的和;链接一系列迭代,建议使用itertools.chain()。
实例
>>> a = [i for i in range(1,101)]
>>> sum(a)
5050
>>> sum(a, 505)
5555
本文介绍了Python的内建函数sum(),它用于从左到右累加序列的元素,可选地加上start值,默认为0。注意start值不能是字符串。文章提到了在特定场景下,可以使用'.join(sequence)'拼接字符串,math.fsum()进行高精度浮点数相加,以及itertools.chain()链接多个迭代器的替代方案。
316

被折叠的 条评论
为什么被折叠?



