Python 添加列表(或集合,或任何合适的数据类型)的元素
有没有一种简单的方法来添加两个大小相等的列表(或元组或任何最有效的数据类型)的成员?
例如,我有 a
和 b
两个元素:
a = (0, 10)
b = (0, -10)
我想添加它们并得到结果:
result = (0, 0)
NOT (0, 10, 0, -10)
Is there an easy way to add the members of two equally sized list
s (or tuple
or whatever data type would work best)?
I have, for example a
and b
with 2 elements:
a = (0, 10)
b = (0, -10)
I want to add them and get as result:
result = (0, 0)
NOT (0, 10, 0, -10)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
您可以使用 numpy 添加列表:
及其结果:
You can use numpy to add the lists:
and the result it:
您可以在 Python 中用一行完成此操作:
示例:
You can do this in one line in Python:
Example:
三个选项:
Three options:
如果你想使用数字列表进行操作,请使用 numpy
if you want to operate with list of numbers use numpy
是的,只需这样做
,或者(实际上明显更快)
计时示例:
yes, just do this
or, (actually clearly faster)
Timing examples: