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

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

问题描述

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总和,为什么不是字符串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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