0

Simple example: I got a list called 'mylist' and I want to accumulate the numbers inside and save them into a new list called 'mylist_accum'.

import numpy

mylist = [1,2,3,4,5]
print mylist

mylist_accum = numpy.add.accumulate(mylist)
print mylist_accum

My prints look like this:

[1, 2, 3, 4, 5]
[ 1  3  6 10 15]

And I want them to look like this:

[1, 2, 3, 4, 5]
[1, 3, 6, 10, 15]

I need my accumulated listelements to be seperated by commas. Otherwise Matplotlib cant work with them.

k.troy2012
  • 344
  • 4
  • 21

1 Answers1

2

It's just printing, matplotlib could handle with the numpy.arrays easy:

In [77]: type(mylist_accum)
Out[77]: numpy.ndarray

If you want to see with commas you could use .tolist method of the numpy.array:

In [75]: mylist_accum.tolist()
Out[75]: [1, 3, 6, 10, 15]

Or convert it to usual list:

In [74]: list(mylist_accum)
Out[74]: [1, 3, 6, 10, 15]
Anton Protopopov
  • 30,354
  • 12
  • 88
  • 93