简体   繁体   English

Python-在列表中添加元组的元素

[英]Python - Adding elements of a tuple in a list

How do I add elements of a tuple in a list? 如何在列表中添加元组的元素?

[(0.50421782178217822, 0.4822060104027705), (0.4375, 0.6666666666666666), (0.375, 0.4642857142857143), (0.26666666666666666, 0.16666666666666666)]

For example: 例如:

=> 0.50421... + 0.4375 + ... => 0.4822... + 0.666 + ... => 0.50421 ... + 0.4375 + ... => 0.4822 ... + 0.666 + ...

It should return a tuple. 它应该返回一个元组。

For example, (0th element sum, 1st element sum). 例如,(第0个元素总和,第1个元素总和)。

The thing that's bothering is the square brackets. 麻烦的是方括号。

I'm stuck here, but don't know how to add the tuples. 我被困在这里,但不知道如何添加元组。

[(x,y) for (x,y) in dict.itervalues()]
>>> map(sum, zip(*mylist))
[1.5833844884488448, 1.779825058021818]

You can do it with the built-in reduce function. 您可以使用内置的reduce函数来实现。

myList = [(0.50421782178217822, 0.4822060104027705), (0.4375, 0.6666666666666666), (0.375, 0.4642857142857143), (0.26666666666666666, 0.16666666666666666)]
reduce(lambda x,y: (x[0]+y[0], x[1]+y[1]), myList)

(1.5833844884488448, 1.779825058021818)

Or with two aggregation variables. 或带有两个聚合变量。

s1,s2 = 0.0, 0.0
for x,y in myList:
   s1+=x
   s2+=y

If functional programming isn't your thing, I decided to give another answer. 如果函数编程不是您的事,那么我决定给出另一个答案。

What we can do is treat the problem as two different problems. 我们可以做的是将问题视为两个不同的问题。 Sum the first entry of each tuple and sum the second entry in each tuple. 将每个元组的第一个条目相加,然后将每个元组的第二个条目相加。 Then combine them into one tuple. 然后将它们合并为一个元组。 Ok, that doesn't sound too bad. 好的,听起来还不错。

Lets start with just summing a list of 1-tuples, well that is just a list. 让我们从对一个1元组的列表求和开始,那只是一个列表。 Summing that is easy. 总结起来很容易。

>>> aList = [1,2,3,4,5]
>>> theSum = sum(aList)
>>> print(theSum)
15

Well that wasn't too bad, makes sense. 嗯,还算不错,这是有道理的。

Now if we had a list of 2-tuples (or n-tuples for that matter) and only wanted the sum of the first entries. 现在,如果我们有一个2元组(或n元组)的列表,并且只想要第一个条目的总和。 Well we could just ignore the rest of the entries as we sum. 好吧,我们求和时可以忽略其余的条目。

>>> aList = [(1,10),(2,10),(3,10),(4,10),(5,10)]
>>> theSum = sum([firstEntry for firstEntry,secondEntry in aList])
>>> print(theSum)
15

Not too bad. 还不错 Now if want to make this a bit more clear we can just clean the second line up. 现在,如果想更清楚一点,我们可以清理第二行。

>>> theSum = sum(firstEntry for firstEntry,_ in aList)

What I have done is taken out the brackets and put an underscore for the unused method. 我所做的是将方括号括起来,并为未使用的方法添加下划线。 This was someone reading the code can see that it is obvious that we don't care about the second item. 有人在阅读代码后可以看到,很明显我们不在乎第二项。

Now what is going on here, what we are doing is do a regular 'for in' loop but our variable is now a name tuple itself, pretty convenient. 现在这里发生了什么,我们正在做的是执行常规的“ for in”循环,但是我们的变量现在是一个名称元组本身,非常方便。

For the answer to the problem 对于问题的答案

>>> aList = [(0.50421782178217822, 0.4822060104027705), (0.4375, 0.6666666666666666), (0.375, 0.4642857142857143), (0.26666666666666666, 0.16666666666666666)]
>>> theSum = (sum(firstEntry for firstEntry,_ in aList),sum(secondEntry for _,secondEntry in aList))
>>> print(theSum)
(1.5833844884488448, 1.779825058021818)

And we are done. 我们完成了。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM