Re: [Numpy-discussion] Slices from an index list

2012-04-11 Thread Warren Weckesser
On Wed, Apr 11, 2012 at 4:28 AM, Mads Ipsen madsip...@gmail.com wrote:

  Hi,

 Suppose a have an array of indices, say

   indices = [0,1,2,3,5,7,8,9,10,12,13,14]

 Then the following slices

   a = slice(0,4)
   b = slice(4,5)
   c = slice(5,9)
   d = slice(9,12)

 provide information about all the consecutive parts of the index list.
 Given the list of indices, is there some nifty numpy function that can
 generate the above slices for me (or their start and stop values)?


Here's one way you could do it:

In [43]: indices = [0,1,2,3,5,7,8,9,10,12,13,14]

In [44]: jumps = where(diff(indices) != 1)[0] + 1

In [45]: starts = hstack((0, jumps))

In [46]: ends = hstack((jumps, len(indices)))

In [47]: slices = [slice(start, end) for start, end in zip(starts, ends)]

In [48]: slices
Out[48]: [slice(0, 4, None), slice(4, 5, None), slice(5, 9, None), slice(9,
12, None)]


Warren
___
NumPy-Discussion mailing list
NumPy-Discussion@scipy.org
http://mail.scipy.org/mailman/listinfo/numpy-discussion


Re: [Numpy-discussion] Slices from an index list

2012-04-11 Thread Zachary Pincus
 Here's one way you could do it:
 
 In [43]: indices = [0,1,2,3,5,7,8,9,10,12,13,14]
 
 In [44]: jumps = where(diff(indices) != 1)[0] + 1
 
 In [45]: starts = hstack((0, jumps))
 
 In [46]: ends = hstack((jumps, len(indices)))
 
 In [47]: slices = [slice(start, end) for start, end in zip(starts, ends)]
 
 In [48]: slices
 Out[48]: [slice(0, 4, None), slice(4, 5, None), slice(5, 9, None), slice(9, 
 12, None)]

If you're only going to use the slices to divide up the list, you could use 
numpy.split and skip creating the slice objects:

indices = [0,1,2,3,5,7,8,9,10,12,13,14]
jumps = numpy.where(numpy.diff(indices) != 1)[0] + 1
numpy.split(indices, jumps)

giving:
[array([0, 1, 2, 3]), array([5]), array([ 7,  8,  9, 10]), array([12, 13, 14])]

Zach

(btw, Warren, the method to calculate the jumps is cute. I'll have to remember 
that.)

___
NumPy-Discussion mailing list
NumPy-Discussion@scipy.org
http://mail.scipy.org/mailman/listinfo/numpy-discussion


Re: [Numpy-discussion] Slices from an index list

2012-04-11 Thread Mads Ipsen

On 11/04/2012 13:42, Warren Weckesser wrote:



On Wed, Apr 11, 2012 at 4:28 AM, Mads Ipsen madsip...@gmail.com 
mailto:madsip...@gmail.com wrote:


Hi,

Suppose a have an array of indices, say

  indices = [0,1,2,3,5,7,8,9,10,12,13,14]

Then the following slices

  a = slice(0,4)
  b = slice(4,5)
  c = slice(5,9)
  d = slice(9,12)

provide information about all the consecutive parts of the index
list. Given the list of indices, is there some nifty numpy
function that can generate the above slices for me (or their start
and stop values)?


Here's one way you could do it:

In [43]: indices = [0,1,2,3,5,7,8,9,10,12,13,14]

In [44]: jumps = where(diff(indices) != 1)[0] + 1

In [45]: starts = hstack((0, jumps))

In [46]: ends = hstack((jumps, len(indices)))

In [47]: slices = [slice(start, end) for start, end in zip(starts, ends)]

In [48]: slices
Out[48]: [slice(0, 4, None), slice(4, 5, None), slice(5, 9, None), 
slice(9, 12, None)]



Warren



___
NumPy-Discussion mailing list
NumPy-Discussion@scipy.org
http://mail.scipy.org/mailman/listinfo/numpy-discussion

Thanks - very helpful!

--
+-+
| Mads Ipsen  |
+--+--+
| Gåsebæksvej 7, 4. tv |  |
| DK-2500 Valby| phone:  +45-29716388 |
| Denmark  | email:  mads.ip...@gmail.com |
+--+--+


___
NumPy-Discussion mailing list
NumPy-Discussion@scipy.org
http://mail.scipy.org/mailman/listinfo/numpy-discussion


Re: [Numpy-discussion] Slices from an index list

2012-04-11 Thread Mads Ipsen

On 11/04/2012 14:19, Zachary Pincus wrote:

Here's one way you could do it:

In [43]: indices = [0,1,2,3,5,7,8,9,10,12,13,14]

In [44]: jumps = where(diff(indices) != 1)[0] + 1

In [45]: starts = hstack((0, jumps))

In [46]: ends = hstack((jumps, len(indices)))

In [47]: slices = [slice(start, end) for start, end in zip(starts, ends)]

In [48]: slices
Out[48]: [slice(0, 4, None), slice(4, 5, None), slice(5, 9, None), slice(9, 12, 
None)]

If you're only going to use the slices to divide up the list, you could use 
numpy.split and skip creating the slice objects:

indices = [0,1,2,3,5,7,8,9,10,12,13,14]
jumps = numpy.where(numpy.diff(indices) != 1)[0] + 1
numpy.split(indices, jumps)

giving:
[array([0, 1, 2, 3]), array([5]), array([ 7,  8,  9, 10]), array([12, 13, 14])]

Zach

(btw, Warren, the method to calculate the jumps is cute. I'll have to remember 
that.)

___
NumPy-Discussion mailing list
NumPy-Discussion@scipy.org
http://mail.scipy.org/mailman/listinfo/numpy-discussion

Thanks - very helpful!

--
+-+
| Mads Ipsen  |
+--+--+
| Gåsebæksvej 7, 4. tv |  |
| DK-2500 Valby| phone:  +45-29716388 |
| Denmark  | email:  mads.ip...@gmail.com |
+--+--+


___
NumPy-Discussion mailing list
NumPy-Discussion@scipy.org
http://mail.scipy.org/mailman/listinfo/numpy-discussion