0

So I have an array of values in which I would like to take out only certain ones to put in a new list or array. To better explain, here is an example of what I'm getting at.

Say I have this array

>>> import numpy as np
>>> x = np.arange(10)
>>> x
array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
>>> 

From this array I would want 0, 2, 3, 5, 6, and 8 in a new list or array. No matter the range of the array, I always want the 1st, next to last, and every two between them. Does anyone have a good way to do this?

So I guess if I explain why I need these values in such a specific way it might help in the solutions. The array I have in my code is a series angles I calculated in previous steps. However, I don't need all the values for the next part of my program.

So far my best guess has been to try and make a dictionary that might assign the values to the angle names I'm looking for.

Something like this...

angle_name = { i=f, i+1=s, i+2=o}
if angle_name.haskey(x[i]):
    somenewlist.append(angle_name[x[i]])

Does that make any sense? My end goal is to eventually pick out the desired angles, name them appropriately and organize them in to a nice looking output.

2 Answers2

0

If the OP wants what described:

import itertools
list(itertools.chain([x[0]], x[2:-2:2], [x[-2]]))

Out[23]: [0, 2, 4, 6, 8]

or by the literal example given:

def pairs_with_gap(arr):
    seq = iter(arr[2:-2])
    yield arr[0]
    for item in seq:
        yield item
        yield next(seq)
        next(seq)
    yield arr[-2]

print [y for y in pairs_with_gap(x)]

Out[26]: [0, 2, 3, 5, 6, 8]

Russia Must Remove Putin
  • 374,368
  • 89
  • 403
  • 331
m.wasowski
  • 6,329
  • 1
  • 23
  • 30
0

May I suggest this approach?

def slice_up(array):
    array_list = list(array)
    return sorted(array_list[2:-2:3] + array_list[:-2:3] + [array_list[-2]])

The slice notation, e.g. [2:-2:3] means that it should start at the 2 index (really the third element, Python indexes start at zero), and go up to, but not including, the 2nd element from the end (-2), by threes. I tried the official docs, but it's not coming up immediately, so here's a canonical Stackoverflow Q&A on slicing: Explain Python's slice notation

and to use:

>>> import numpy
>>> slice_up(numpy.arange(10))
[0, 2, 3, 5, 6, 8]
>>> slice_up(numpy.arange(15))
[0, 2, 3, 5, 6, 8, 9, 11, 12, 13]
>>> slice_up(numpy.arange(20))
[0, 2, 3, 5, 6, 8, 9, 11, 12, 14, 15, 17, 18]
Community
  • 1
  • 1
Russia Must Remove Putin
  • 374,368
  • 89
  • 403
  • 331
  • This is really helpful. I'm still learning many of the different sorting, splitting, joining style tools in python. I'm a little bit confused about the array_list[2:-2:3]. What does the value in between the colons (:-2:) do as far as picking out values in the array? – user3418064 Mar 14 '14 at 03:14
  • @user3418064 I commented about the slice notation, does that explain it? – Russia Must Remove Putin Mar 14 '14 at 03:14
  • yes, this makes so much more sense and is way less convoluted than what I was trying to do. thanks – user3418064 Mar 14 '14 at 03:28