Hey, I would like to be able to perform this but with being selective for which lists I sum up. Let's say, that same example, but with only adding up the first number from the 3rd and 4th list.
-
4You really can't adapt that answer to your problem? And it's not clear what exactly you want. Will it always be a continuous range? Any reason you need the 3rd and 4th? Why not 3rd and 5th? – Falmarri Jan 11 '11 at 21:53
-
It's because I have each list within the tuple containing a date and closing price for a specific stock. To calculate SMA10 for 30 days ago, I need the sum of the closing price for tuple location 30 to 39. – Jared Jan 11 '11 at 22:12
-
My bad, they are strings. I got it to work by doing this. for close in tickers[30:39]: intclose = float(close[4]) sumclo += intclose – Jared Jan 11 '11 at 22:53
4 Answers
Something like:
sum(int(tuple_list[i][0]) for i in range(3,5))
range(x, y) generates a list of integers from x(included) to y(excluded) and 1 as the step. If you want to change the range(x, y, step)
will do the same but increasing by step.
You can find the official documentation here
Or you can do:
sum(float(close[4]) for close in tickers[30:40])

- 6,630
- 30
- 46
-
1Rather than construct an intermediate list, just use a generator expression by omitting the braces: `sum(tuple_list[i][0] for i in range(3,5))` – Seth Johnson Jan 11 '11 at 22:16
-
This looks like it shall work, but i just realized that prices I have in there are ints. What should I add to convert them? – Jared Jan 11 '11 at 22:32
-
1I don't get what you are asking, if they are ints you shouldn't have problems to sum them up. – Santiago Alessandri Jan 11 '11 at 22:36
>>> l1
[(0, 2), (1, 3), (2, 4), (3, 5), (4, 6), (5, 7), (6, 8), (7, 9), (8, 10), (9, 11)]
>>> sum([el[0] for (nr, el) in enumerate(l1) if nr in [3, 4]])
7
>>>

- 6,568
- 2
- 29
- 26
If you want to limit by some property of each element, you can use filter()
before feeding it to the code posted in your link. This will let you write a unique filter depending on what you want. This doesn't work for the example you gave, but it seemed like you were more interested in the general case.
sum(pair[0] for pair in filter(PREDICATE_FUNCTION_OR_LAMBDA, list_of_pairs))

- 17,141
- 7
- 47
- 64
not seen an answer using reduce yet.
reduce(lambda sumSoFar,(tuple0,tuple1): sumSoFar+tuple0, list, 0)
In essence sum is identical to reduce(int.__add__, list, 0)
edit: didn't read the predicate part.
Easily fixed, but probably not the best answer anymore:
predicate = lambda x: x == 2 or x == 4
reduce(lambda sumSoFar,(t0,t1): sumSoFar+(t0 if predicate(t0) else 0), list, 0)

- 37,291
- 7
- 81
- 97
-
1Can we please stop using reduce in Python? Obfuscation and poor algorithmic complexity are not our goals. – Seth Johnson Jan 11 '11 at 22:17
-
Without taking the the conditional part of the summation into account I do not see why reduce would be a poor choice. It iterates over a list applying a given function to each each element in turn. Saying reduce is a poor choice is like like saying map is a poor choice for applying a function to every element in a list. – Dunes Jan 11 '11 at 22:26
-
'reduce' obfuscates? Try telling that to everyone who uses a real FP language. 'reduce' has poor algorithmic complexity? As far as I can tell it is O(N). – Karl Knechtel Jan 11 '11 at 22:27