python sum 函数 - 需要`start`参数解释

python sum function - `start` parameter explanation required(python sum 函数 - 需要`start`参数解释)
本文介绍了python sum 函数 - 需要`start`参数解释的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

我正在尝试了解内置 sum() 函数的工作原理,但是,start 参数让我失去了理智:

I am trying to understand the working of the built-in sum() function, but, the start parameter has evaporated my mind:

a=[[1, 20], [2, 3]]
b=[[[[[[1], 2], 3], 4], 5], 6]
>>> sum(b,a)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: can only concatenate list (not "int") to list
>>> sum(a,b)
[[[[[[1], 2], 3], 4], 5], 6, 1, 20, 2, 3]

  • >>> a=[1,2]
    >>> b=[3,4]
    >>> sum(a,b)
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
    TypeError: can only concatenate list (not "int") to list
    >>> sum(b,a)
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
    TypeError: can only concatenate list (not "int") to list
    

  • 我对此感到目瞪口呆,不知道发生了什么.以下是 python 文档的内容:http://docs.python.org/library/functions.html#sum.这并没有给出关于如果开始不是字符串而不是整数怎么办?"的任何解释

    I am just dumbfounded by this and don't have any idea what is happening. Here is what the python docs have to say: http://docs.python.org/library/functions.html#sum. This does not give any explanation on 'what if the start is not a string and not an integer?'

    推荐答案

    Sum 做了这样的事情

    Sum does something like this

    def sum(values, start = 0):
        total = start
        for value in values:
            total = total + value
        return total
    

    sum([1,2],[3,4]) 扩展了类似 [3,4] + 1 + 2 的内容,您可以看到它试图将数字和列表相加.

    sum([1,2],[3,4]) expands something like [3,4] + 1 + 2, which you can see tries to add numbers and lists together.

    为了使用 sum 生成列表,值应该是列表的列表,而 start 可以只是一个列表.您会在失败的示例中看到该列表至少包含一些整数,而不是所有列表.

    In order to use sum to produce lists, the values should be a list of lists, whereas start can be just a list. You'll see in your failing examples that the list contains at least some ints, rather then all lists.

    您可能会想到将 sum 与列表一起使用的通常情况是将列表列表转换为列表

    The usual case where you might think of using sum with lists is to convert a list of lists into a list

    sum([[1,2],[3,4]], []) == [1,2,3,4]
    

    但实际上你不应该这样做,因为它会很慢.

    But really you shouldn't do that, as it'll be slow.

    这篇关于python sum 函数 - 需要`start`参数解释的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

    本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

    相关文档推荐

    patching a class yields quot;AttributeError: Mock object has no attributequot; when accessing instance attributes(修补类会产生“AttributeError:Mock object has no attribute;访问实例属性时)
    How to mock lt;ModelClassgt;.query.filter_by() in Flask-SqlAlchemy(如何在 Flask-SqlAlchemy 中模拟 lt;ModelClassgt;.query.filter_by())
    FTPLIB error socket.gaierror: [Errno 8] nodename nor servname provided, or not known(FTPLIB 错误 socket.gaierror: [Errno 8] nodename nor servname provided, or not known)
    Weird numpy.sum behavior when adding zeros(添加零时奇怪的 numpy.sum 行为)
    Why does the #39;int#39; object is not callable error occur when using the sum() function?(为什么在使用 sum() 函数时会出现 int object is not callable 错误?)
    How to sum in pandas by unique index in several columns?(如何通过几列中的唯一索引对 pandas 求和?)