Python sum,为什么不是字符串? [英] Python sum, why not strings?

查看:19
本文介绍了Python sum,为什么不是字符串?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Python 有一个内置函数 sum,它等效于:

Python has a built in function sum, which is effectively equivalent to:

def sum2(iterable, start=0):
    return start + reduce(operator.add, iterable)

用于除字符串之外的所有类型的参数.它适用于数字和列表,例如:

for all types of parameters except strings. It works for numbers and lists, for example:

 sum([1,2,3], 0) = sum2([1,2,3],0) = 6    #Note: 0 is the default value for start, but I include it for clarity
 sum({888:1}, 0) = sum2({888:1},0) = 888

为什么要特别省略字符串?

Why were strings specially left out?

 sum( ['foo','bar'], '') # TypeError: sum() can't sum strings [use ''.join(seq) instead]
 sum2(['foo','bar'], '') = 'foobar'

因为这个原因,我似乎记得 Python 列表中的讨论,所以一个解释或一个指向解释它的线程的链接会很好.

I seem to remember discussions in the Python list for the reason, so an explanation or a link to a thread explaining it would be fine.

编辑:我知道标准方法是执行 "".join.我的问题是为什么禁止对字符串使用 sum 的选项,而对于列表等则没有禁止.

Edit: I am aware that the standard way is to do "".join. My question is why the option of using sum for strings was banned, and no banning was there for, say, lists.

编辑 2:虽然我认为鉴于我得到的所有好的答案不需要这样做,但问题是:为什么 sum 对包含数字的可迭代对象或包含列表的可迭代对象起作用,但是不是包含字符串的可迭代对象?

Edit 2: Although I believe this is not needed given all the good answers I got, the question is: Why does sum work on an iterable containing numbers or an iterable containing lists but not an iterable containing strings?

推荐答案

Python 试图阻止您对字符串进行求和".你应该加入他们:

Python tries to discourage you from "summing" strings. You're supposed to join them:

"".join(list_of_strings)

速度更快,占用的内存更少.

It's a lot faster, and uses much less memory.

快速基准:

$ python -m timeit -s 'import operator; strings = ["a"]*10000' 'r = reduce(operator.add, strings)'
100 loops, best of 3: 8.46 msec per loop
$ python -m timeit -s 'import operator; strings = ["a"]*10000' 'r = "".join(strings)'
1000 loops, best of 3: 296 usec per loop

编辑(回答 OP 的编辑):至于为什么字符串显然被挑出",我相信这只是针对常见情况进行优化以及强制执行最佳实践的问题:您可以更快地加入字符串''.join,因此明确禁止 sum 上的字符串将向新手指出这一点.

Edit (to answer OP's edit): As to why strings were apparently "singled out", I believe it's simply a matter of optimizing for a common case, as well as of enforcing best practice: you can join strings much faster with ''.join, so explicitly forbidding strings on sum will point this out to newbies.

顺便说一句,这个限制已经永远"存在了,也就是说,因为 sum 是作为内置函数添加的(rev. 32347)

BTW, this restriction has been in place "forever", i.e., since the sum was added as a built-in function (rev. 32347)

这篇关于Python sum,为什么不是字符串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆