lundi 13 avril 2015

Summing the first value in each tuple in a list over a specific range in Python


Vote count:

0




My question is based on an earlier question and I can't find to figure out (nor find) the subsequent step I need.


Say I do have the same list of tuples, namely:



[(0, 1), (2, 3), (5, 7), (2, 1)]


Also I wish to find the sum of the first values in each pair, which could be done by the simple pythonic:



sum([pair[0] for pair in list_of_pairs])


as provided by David Z. However, in my case I only wish to sum from the first first value up until a following first value, say the first value at index N = 2. Thus, only calculate the sum of:



0 + 2 + 5


I have been trying things like:



sum([pair[0] for pair in list_of_pairs[:,N])


but without success. Can anyone provide me with an elegant solution?



asked 1 min ago







Summing the first value in each tuple in a list over a specific range in Python

Aucun commentaire:

Enregistrer un commentaire