Python 存储在元组中的元素总和

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/29471260/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 04:37:42  来源:igfitidea点击:

Sum of elements stored inside a tuple

pythonpython-2.7

提问by Manas Chaturvedi

Given a tuple containing a bunch of integer elements, how can one find the sum of all the elements?

给定一个包含一堆整数元素的元组,如何找到所有元素的总和?

For example, if I have a list of tuples:

例如,如果我有一个元组列表:

li = [(1, 2), (1, 3), (2, 3)]

How can I get something like this:

我怎样才能得到这样的东西:

[3, 4, 5]

where 3, 4 and 5 is the total sum of each of the three tuples respectively?

其中 3、4 和 5 分别是三个元组中每个元组的总和?

采纳答案by thefourtheye

You can use mapand sumfunction like this

您可以像这样使用mapsum运行

>>> li = [(1, 2), (1, 3), (2, 3)]
>>> map(sum, li)
[3, 4, 5]

Alternatively you can use list comprehension, like this

或者,您可以使用列表理解,就像这样

>>> [sum(tup) for tup in li]
[3, 4, 5]

Note:I personally prefer the list comprehension version, because mapfunction in Python 3.x will return an iterable mapobject, which needs to be explicitly converted to a list, like this list(map(sum, li)).

注意:我个人更喜欢列表推导式版本,因为mapPython 3.x 中的函数将返回一个可迭代map对象,该对象需要显式转换为列表,如下所示list(map(sum, li))

>>> li = [(1, 2), (1, 3), (2, 3)]
>>> map(sum, li)
<map object at 0x7f3dc25bb0f0>
>>> type(map(sum, li))
<class 'map'>
>>> list(map(sum, li))
[3, 4, 5]

But list comprehension will give a list in both Python 2.x and Python 3.x.

但是列表理解将在 Python 2.x 和 Python 3.x 中给出一个列表。

回答by Avinash Raj

You could use list comprehension.

您可以使用列表理解

>>> li = [(1, 2), (1, 3), (2, 3)]
>>> [x+y for (x,y) in li]
[3, 4, 5]

回答by Vivek Sable

For beginner:

初学者:

  1. Create result variable which type is list.
  2. Iterate every item from the give list by forloop.
  3. As every item is tuple so again iterate item from the step 2 and set sum of item to 0.
  4. Add sum.
  5. Append sum to result variable.
  1. 创建类型为列表的结果变量。
  2. 通过循环迭代给定列表中的每个项目for
  3. 由于每个项目都是元组,因此再次从步骤 2 迭代项目并将项目总和设置为 0。
  4. 加总。
  5. 将 sum 附加到结果变量。

Demo:

演示:

>>> li = [(1, 2), (1, 3), (2, 3)]   # Given Input
>>> result = []                     # Step 1
>>> for i in li:                    # Step 2
...     tmp_sum = 0                 # Step 3  
...     for j in i:                 # Step 3
...         tmp_sum += j            # Step 4 
...     result.append(tmp_sum)      # Step 5 
... 
>>> print result
[3, 4, 5]


回答by Kishor Subedi

ls= [(1,2), (3,4)]
finallist = []
for tuple in ls:
     listt = list(tuple)
     summ = 0
     for m in listt:
         summ+=m
     finallist.append(summ)
print(finallist) #[3,7]

回答by JAID

Both solutions below will work.

下面的两种解决方案都有效。

li = [(1, 2), (1, 3), (2, 3)]
print([sum(i) for i in li])

or

或者

def sumtupleinlist(lst):
    return [sum(i) for i in lst]
li = [(1, 2), (1, 3), (2, 3)]

To test the function, run :

要测试该功能,请运行:

print(sumtupleinlist(li))