0

I would like to get a list of the following indices:

0...8,100...108,200...208...

How can this be achieved, in order to slice an array, i.e: arr[???] (without numpy, try to be as memory efficient as possible)

Update 1: This can be achieved with the following code:

np.hstack([np.arange(i*100,i*100+9) for i in range(10)])

I would like to achieve the same results, without numpy

Mercury
  • 1,886
  • 5
  • 25
  • 44
  • 1
    Please, post your attempt. – go2nirvana Nov 13 '20 at 10:21
  • "How can this be achieved, in order to slice an array, i.e: arr[???] (without numpy" - wait, what? What do you even mean, if you're not using NumPy? Neither lists nor any of the standard library types named "array" support any form of indexing that looks anything like what you're talking about. – user2357112 Nov 13 '20 at 10:21
  • you can find a similar question [here](https://stackoverflow.com/questions/38895781/multiple-slices-with-python) – Hasim Sk Nov 13 '20 at 10:30
  • @user2357112supportsMonica: I have a list/tuple, and I would like to access all adjacent 9 elements every 100 elements. With nupy I would just create the indices list as in the example above, at access it. I would like to do the same without numpy – Mercury Nov 13 '20 at 10:54

2 Answers2

0

You can add lists and using your slicing indices separately, for e.g.:

x = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
y = x[0:2] + x[6:9]

print(y)
>> [1, 2, 7, 8, 9]

As suggested in the comments: Multiple Slices With Python

lrsp
  • 1,213
  • 10
  • 18
0

You may use list comprehension with conditions to exclude certain range of values:

my_list = [x for x in range(0,309) if x not in range(9, 100) and x not in range(109, 200) and x not in range(209, 300)]

print(my_list)

Result

[0, 1, 2, 3, 4, 5, 6, 7, 8, 100, 101, 102, 103, 104, 105, 106, 107, 108, 200, 201, 202, 203, 204, 205, 206, 207, 208, 300, 301, 302, 303, 304, 305, 306, 307, 308]

UPDATE

For any interval (0, N), you may use a while loop to generate values in the pattern requested like this:

n = 908 #any integer
my_list = []
i = 0
while i < n:
    for i in range(i, i+9):
        my_list.append(i)
        i += 92
print(my_list)

Result

[0, 1, 2, 3, 4, 5, 6, 7, 8, 100, 101, 102, 103, 104, 105, 106, 107, 108, 200, 201, 202, 203, 204, 205, 206, 207, 208, 300, 301, 302, 303, 304, 305, 306, 307, 308, 400, 401, 402, 403, 404, 405, 406, 407, 408, 500, 501, 502, 503, 504, 505, 506, 507, 508, 600, 601, 602, 603, 604, 605, 606, 607, 608, 700, 701, 702, 703, 704, 705, 706, 707, 708, 800, 801, 802, 803, 804, 805, 806, 807, 808, 900, 901, 902, 903, 904, 905, 906, 907, 908]
Seyi Daniel
  • 2,259
  • 2
  • 8
  • 18