python - Select all possible subarrays of length n -
how can 2d array containing possible consecutive sub-arrays of length?
for example, array ['a', 'b', 'c', 'd', 'e']
, , n 3, result should be
[['a', 'b', 'c'] ['b', 'c', 'd'] ['c', 'd', 'e']]
i found similar question relating python lists, i'd numpy, need perform on many different arrays, each of large. basically, speed issue here.
third , final no-loop answer:
def substrings(n, x) return numpy.fromfunction(lambda i, j: x[i + j], (len(x) - n + 1, n), dtype=int)
you'll have profile of these solutions find 1 that's performant. if 1 of these solutions best, please select correct answer.
Comments
Post a Comment