could sum be faster on lists

后端 未结 3 1235
一生所求
一生所求 2020-12-07 02:11

This is somehow a follow-up to this question

So first, you\'ll notice that you cannot perform a sum on a list of strings to concatenate them, python tel

3条回答
  •  -上瘾入骨i
    2020-12-07 02:34

    /* It's tempting to use PyNumber_InPlaceAdd instead of
               PyNumber_Add here, to avoid quadratic running time
               when doing 'sum(list_of_lists, [])'.  However, this
               would produce a change in behaviour: a snippet like
                 empty = []
                 sum([[x] for x in range(10)], empty)
               would change the value of empty. */
    temp = PyNumber_Add(result, item);
    

    Taken from Python's built-in source code https://github.com/python/cpython/blob/master/Python/bltinmodule.c#L2146 Line:2342

提交回复
热议问题