[Numpy-discussion] List/location of consecutive integers

Andrea Gavana andrea.gavana@gmail....
Fri May 22 11:31:19 CDT 2009

Hi All,

    this should be a very easy question but I am trying to make a
script run as fast as possible, so please bear with me if the solution
is easy and I just overlooked it.

I have a list of integers, like this one:

indices = [1,2,3,4,5,6,7,8,9,255,256,257,258,10001,10002,10003,10004]

>From this list, I would like to find out which values are consecutive
and store them in another list of tuples (begin_consecutive,
end_consecutive) or a simple list: as an example, the previous list
will become:

new_list = [(1, 9), (255, 258), (10001, 10004)]

I can do it with for loops, but I am trying to speed up a fotran-based
routine which I wrap with f2py (ideally I would like to do this step
in Fortran too, so if you have a suggestion on how to do it also in
Fortran it would be more than welcome). Do you have any suggestions?

Thank you for your time.


"Imagination Is The Only Weapon In The War Against Reality."

More information about the Numpy-discussion mailing list