-1

I have a numpy array: np.array([0, 93, 94, 96, 228]), which I need to split into multiple lists like:

[0,93]
[93,94]
[94,96]
[96,228]

How can I solve this in python?

KetZoomer
  • 2,701
  • 3
  • 15
  • 43
  • This question may have been answered already. Does https://stackoverflow.com/questions/24483182/python-split-list-into-n-chunks/29679492 do what you need? – David Weiser Dec 01 '20 at 16:39
  • Does this answer your question? [Python split list into n chunks](https://stackoverflow.com/questions/24483182/python-split-list-into-n-chunks) – Owen Kelvin Dec 01 '20 at 17:29

1 Answers1

2

Something like this will do:

Firstly import numpy,

import numpy as np

Next, assign a variable to your numpy array,

a = np.array([ 0,  93,  94,  96, 228])

Create an empty list to store the desired output,

b=[]

Append elements and creating new lists,

for i in range(0, len(a)-1):
    b.append([a[i], a[i+1]])

And then the print the output

for each in b: 
    print(each)

Doing so, gives the desired output.

KetZoomer
  • 2,701
  • 3
  • 15
  • 43