I'm trying to write code to firstly, order numbers from lowest to highest (e.g. 1, 3, 2, 4, 5 to 1, 2, 3, 4, 5). Secondly, I would like to incrementally add the numbers in the list. eg.
1
3
6
10
15
I've already tried using the sum
function, then the sorted
function, but I was wondering if I can write them neatly in a code to just get everything worked out.
Addition = [1, 13, 166, 3, 80, 6, 40]
print(sorted(Addition))
I was able to get the numbers sorted horizontally, but I wasn't able to get the numbers added vertically.
You can use
itertools.accumulate
withsorted
:The default action is
operator.add
, but you can customize it. For example, you can do running product instead of running sum if you needed it: