I have a list of tuples (always pairs) like this:
我有一个像这样的元组列表(总是对):
[(0, 1), (2, 3), (5, 7), (2, 1)]
I'd like to find the sum of the first items in each pair, i.e.:
我想找到每对中第一项的总和,即:
0 + 2 + 5 + 2
How can I do this in Python? At the moment I'm iterating through the list:
我怎么能用Python做到这一点?目前我正在遍历列表:
sum = 0
for pair in list_of_pairs:
sum += pair[0]
I have a feeling there must be a more Pythonic way.
我觉得必须有更多的Pythonic方式。
7 个解决方案
#1
54
A version compatible with Python 2.3 is
与Python 2.3兼容的版本是
sum([pair[0] for pair in list_of_pairs])
or in recent versions of Python, see this answer or this one.
或者在Python的最新版本中,请参阅此答案或此答案。
#2
33
sum(i for i, j in list_of_pairs)
will do too.
也会这样做。
#3
14
I recommend:
sum(i for i, _ in list_of_pairs)
Note:
Using the variable _
(or __
to avoid confliction with the alias of gettext
) instead of j
has at least two benefits:
使用变量_(或__以避免与gettext的别名冲突)而不是j具有至少两个好处:
-
_
(which stands for placeholder) has better readability -
pylint
won't complain: "Unused variable 'j'"
_(代表占位符)具有更好的可读性
pylint不会抱怨:“未使用的变量'j'”
#4
5
If you have a very large list or a generator that produces a large number of pairs you might want to use a generator based approach. For fun I use itemgetter()
and imap()
, too. A simple generator based approach might be enough, though.
如果您有一个非常大的列表或生成大量对的生成器,您可能希望使用基于生成器的方法。为了好玩,我也使用itemgetter()和imap()。但是,基于简单生成器的方法可能就足够了。
import operator
import itertools
idx0 = operator.itemgetter(0)
list_of_pairs = [(0, 1), (2, 3), (5, 7), (2, 1)]
sum(itertools.imap(idx0, list_of_pairs)
Edit: itertools.imap() is available in Python 2.3. So you can use a generator based approach there, too.
编辑:itertools.imap()在Python 2.3中可用。所以你也可以在那里使用基于生成器的方法。
#5
3
Obscure (but fun) answer:
模糊(但有趣)答案:
>>> sum(zip(*list_of_pairs)[0])
9
Or when zip's are iterables only this should work:
或者当zip是迭代时,这应该是有用的:
>>> sum(zip(*list_of_pairs).__next__())
9
#6
0
Below is sample code, you can also specify the list range.
下面是示例代码,您还可以指定列表范围。
def test_lst_sum():
lst = [1, 3, 5]
print sum(lst) # 9
print sum(lst[1:]) # 8
print sum(lst[5:]) # 0 out of range so return 0
print sum(lst[5:-1]) # 0
print sum(lst[1: -1]) # 3
lst_tp = [('33', 1), ('88', 2), ('22', 3), ('44', 4)]
print sum(x[1] for x in lst_tp[1:]) # 9
#7
0
If you don't mind converting it to a numpy array, you can use np.sum
over axis=0
as given here
如果你不介意将它转换为numpy数组,你可以使用np.sum over axis = 0,如下所示
#1
54
A version compatible with Python 2.3 is
与Python 2.3兼容的版本是
sum([pair[0] for pair in list_of_pairs])
or in recent versions of Python, see this answer or this one.
或者在Python的最新版本中,请参阅此答案或此答案。
#2
33
sum(i for i, j in list_of_pairs)
will do too.
也会这样做。
#3
14
I recommend:
sum(i for i, _ in list_of_pairs)
Note:
Using the variable _
(or __
to avoid confliction with the alias of gettext
) instead of j
has at least two benefits:
使用变量_(或__以避免与gettext的别名冲突)而不是j具有至少两个好处:
-
_
(which stands for placeholder) has better readability -
pylint
won't complain: "Unused variable 'j'"
_(代表占位符)具有更好的可读性
pylint不会抱怨:“未使用的变量'j'”
#4
5
If you have a very large list or a generator that produces a large number of pairs you might want to use a generator based approach. For fun I use itemgetter()
and imap()
, too. A simple generator based approach might be enough, though.
如果您有一个非常大的列表或生成大量对的生成器,您可能希望使用基于生成器的方法。为了好玩,我也使用itemgetter()和imap()。但是,基于简单生成器的方法可能就足够了。
import operator
import itertools
idx0 = operator.itemgetter(0)
list_of_pairs = [(0, 1), (2, 3), (5, 7), (2, 1)]
sum(itertools.imap(idx0, list_of_pairs)
Edit: itertools.imap() is available in Python 2.3. So you can use a generator based approach there, too.
编辑:itertools.imap()在Python 2.3中可用。所以你也可以在那里使用基于生成器的方法。
#5
3
Obscure (but fun) answer:
模糊(但有趣)答案:
>>> sum(zip(*list_of_pairs)[0])
9
Or when zip's are iterables only this should work:
或者当zip是迭代时,这应该是有用的:
>>> sum(zip(*list_of_pairs).__next__())
9
#6
0
Below is sample code, you can also specify the list range.
下面是示例代码,您还可以指定列表范围。
def test_lst_sum():
lst = [1, 3, 5]
print sum(lst) # 9
print sum(lst[1:]) # 8
print sum(lst[5:]) # 0 out of range so return 0
print sum(lst[5:-1]) # 0
print sum(lst[1: -1]) # 3
lst_tp = [('33', 1), ('88', 2), ('22', 3), ('44', 4)]
print sum(x[1] for x in lst_tp[1:]) # 9
#7
0
If you don't mind converting it to a numpy array, you can use np.sum
over axis=0
as given here
如果你不介意将它转换为numpy数组,你可以使用np.sum over axis = 0,如下所示