总结一个数字列表比与for循环更快的方法? [英] Faster way to sum a list of numbers than with a for-loop?

查看:144
本文介绍了总结一个数字列表比与for循环更快的方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有办法总结一个数字列表比使用for-loop更快,也许在Python库中?或者是真的只有多线程/矢量处理可以有效地做到这一点?编辑:只是澄清,它可能是任何数字的列表,未分类,只是输入来自用户。

解决方案

你可以使用sum()来求和一个数组的值。 b

  a = [1,9,12] 
印刷金额(a)


Is there a way to sum up a list of numbers faster than with a for-loop, perhaps in the Python library? Or is that something really only multi-threading / vector processing can do efficiently?

Edit: Just to clarify, it could be a list of any numbers, unsorted, just input from the user.

解决方案

You can use sum() to sum the values of an array.

a = [1,9,12]
print sum(a)

这篇关于总结一个数字列表比与for循环更快的方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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