0

I have a list of lists. For example:

a = [[38, 2, 33, 8, 17, 8, 39, 36, 34, 17, 26, 22, 10, 2, 37, 17, 33, 2, 23, 40, 38, 0, 40, 14, 3, 30],
[38, 20, 31, 33, 0, 30, 33, 2, 8, 34, 30, 36, 10, 2, 38, 35, 8, 40, 0, 25, 2, 30, 2, 25]]

This list has about 200 sub-lists (I've provided 2). I am looking for a way to output an array containing a sliding window with an arbitrary before and after number of values. For example, with two preceding values and three following values, I am trying to get:

[[_, _, 38, 2, 33, 8],
[_, 38, 2, 33, 8, 17],
[38, 2, 33, 8, 17, 9],
...,
[14, 3, 30, _, _, _,],
[_, _, 38, 20, 31, 33],
...]

I will need to repeat this operation many times (for different sub-lists), and thus speed is important. I am under the impression that converting the data to a Numpy array to use the answer here may be too slow (since each list is of a different length I am guessing I need to instantiate multiple np.arrays). Is there a nice way to do this? Thank you!

purpleostrich
  • 235
  • 1
  • 2
  • 6
  • I don't understand the last two lines of your "sliding" array. From the first three it looks like you have the array`a[0]` moving to the left (although they differ at `a[5]`, as `a[5] = 8`) but then the two last lines seem to contradict that, as `[14, 3, 30, _, _, _,]` looks utterly unrelated to `[_, _, 38, 20, 31, 33]`, so I have no idea what you are trying to achieve. – Recessive Apr 18 '19 at 04:05
  • @Recessive The last two lines are from the last part of the first sublist and the first part of the second sublist. – JohanL Apr 18 '19 at 04:55
  • 2
    What would be those `_`, because `_` aren't recognized as valid scalars in a regular array. – Divakar Apr 18 '19 at 05:23

1 Answers1

0

I am fairly sure this is the fastest way of doing it.

def f(b):
    ret = []

    for sublist in b:
        ret.append(["_","_"] + sublist[0:4])
        ret.append(["_"] + sublist[0:5])
        for i in range(len(sublist)-4):
            ret.append(sublist[i:i+5])
        ret.append(sublist[-4:] + ["_"])
        ret.append(sublist[-3:] + ["_", "_"])
        ret.append(sublist[-2:] + ["_", "_", "_"])
    return ret

Obviously you have to iterate over every sublist in order to actually achieve the desired result, and for each sublist you have to iterate over it n times in order ot produce n lists, so I'm fairly sure this (o(n^2)) is the fastest you will get it.

Recessive
  • 1,780
  • 2
  • 14
  • 37