N的总和列出了元素方式的python

栏目: Python · 发布时间: 7年前

内容简介:翻译自:https://stackoverflow.com/questions/19261747/sum-of-n-lists-element-wise-python
有没有一种简单的方法来计算 python

中N个列表的元素总和?我知道如果我们定义了n个列表(调用第i个列表c_i),我们可以这样做:

z = [sum(x) for x in zip(c_1, c_2, ...)]

例如:

c1 = [1,2]
c2 = [3,4]
c3 = [5,6]
z  = [sum(x) for x in zip(c1,c2,c3)]

这里z = [9,12]

但是如果我们没有定义c_i而在列表C中有c_1 … c_n呢?

如果我们只有C,是否有类似的方法来找到z?

我希望这很清楚.

已解决:我想知道*操作符的全部内容……谢谢!

这样做:
[sum(x) for x in zip(*C)]

在上面,C是c_1 … c_n的列表.正如评论中的 link 所述(谢谢,@ kevinsa5!):

* is the “splat” operator: It takes a list as input, and expands it into actual positional arguments in the function call.

有关其他详细信息,请查看 documentation ,在“解压参数列表”下,并阅读有关 calls (谢谢,@ abarnert!)

翻译自:https://stackoverflow.com/questions/19261747/sum-of-n-lists-element-wise-python


以上所述就是小编给大家介绍的《N的总和列出了元素方式的python》,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对 码农网 的支持!

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

Learning Web Design

Learning Web Design

Jennifer Niederst Robbins / O'Reilly Media / 2007-6-15 / USD 44.99

Since the last edition of this book appeared three years ago, there has been a major climate change with regard to web standards. Designers are no longer using (X)HTML as a design tool, but as a means......一起来看看 《Learning Web Design》 这本书的介绍吧!

RGB转16进制工具
RGB转16进制工具

RGB HEX 互转工具

URL 编码/解码
URL 编码/解码

URL 编码/解码

RGB HSV 转换
RGB HSV 转换

RGB HSV 互转工具