0

I have an output as a list on which I need to perform some operations. For that, I converted into an array using np.asarrayfunction .

My list

[[ 0.00000000e+00,  0.00000000e+00,  0.00000000e+00,
        -5.41511240e+01, -2.30953821e+01, -1.74871288e+01,
        -4.91000564e+01,  2.44382720e+02,  1.71213983e+02,
        -1.54755310e+02,  5.97881714e+02,  3.52366218e+02,
        -1.15560633e+01,  7.42149725e+02,  1.66477287e+02,
        -1.18447102e+01,  7.36763064e+02,  1.65182437e+02,
         5.41502771e+01,  2.30950820e+01,  1.74870470e+01,
        -1.71258190e+01,  3.26592894e+02,  2.03220778e+02,
        -1.41130065e+02,  7.04033786e+02,  3.84201614e+02,
        -1.12642400e+01,  7.48636864e+02,  1.66665977e+02,
        -1.14090840e+01,  7.36435064e+02,  1.63713810e+02,
         1.21660845e-03, -8.60110629e-02, -1.93000576e-02,
         1.57141460e+01, -2.34399996e+02, -2.86722926e+01,
         5.28252697e+01, -4.40469167e+02, -1.11653705e+02,
         1.03085631e+02, -5.01280352e+02, -1.93111585e+02,
         7.16011844e+01, -5.88214725e+02, -2.18615940e+02,
         5.67537804e+00, -4.35088906e+02, -9.76974016e+01,
         7.71909591e+01, -3.88738749e+02, -6.29099586e+01,
        -2.99970496e+01, -2.25985794e+02,  6.41590789e+01,
         1.03847001e+02, -7.32419021e+01,  1.04802558e+02,
         1.26585822e+00, -1.20170579e+02, -2.82526049e+01,
         1.57900698e+00, -1.51780249e+02, -3.52080548e+01,
         8.84543993e-01, -1.07795356e+02, -2.56307189e+01,
         8.84543993e-01, -1.07795356e+02, -2.56307189e+01,
         5.67537804e+00, -4.35088906e+02, -9.76974016e+01,
         8.01141013e+00, -4.16078607e+02, -1.25355227e+02,
         1.17740492e+00, -2.55151916e+02, -7.20503620e+01,
        -1.73992688e+01, -2.44854505e+02, -9.25408725e+01,
         8.70569014e-01, -1.68664569e+02, -3.73902498e+01,
         1.39982512e+00, -2.00884252e+02, -4.47207875e+01,
         5.24591115e-01, -1.65867774e+02, -3.68342864e+01,
         5.24591115e-01, -1.65867774e+02, -3.68342864e+01]])]

I had planned to extract the first column and create a vector x extract second column vector y and third column vector z. Howver when I converted it into an array , it resulted into 1x1x96array type with the following structure

[[[ 0.00000000e+00  0.00000000e+00  0.00000000e+00 -5.41511240e+01
   -2.30953821e+01 -1.74871288e+01 -4.91000564e+01  2.44382720e+02
    1.71213983e+02 -1.54755310e+02  5.97881714e+02  3.52366218e+02
   -1.15560633e+01  7.42149725e+02  1.66477287e+02 -1.18447102e+01
    7.36763064e+02  1.65182437e+02  5.41502771e+01  2.30950820e+01
    1.74870470e+01 -1.71258190e+01  3.26592894e+02  2.03220778e+02
   -1.41130065e+02  7.04033786e+02  3.84201614e+02 -1.12642400e+01
    7.48636864e+02  1.66665977e+02 -1.14090840e+01  7.36435064e+02
    1.63713810e+02  1.21660845e-03 -8.60110629e-02 -1.93000576e-02
    1.57141460e+01 -2.34399996e+02 -2.86722926e+01  5.28252697e+01
   -4.40469167e+02 -1.11653705e+02  1.03085631e+02 -5.01280352e+02
   -1.93111585e+02  7.16011844e+01 -5.88214725e+02 -2.18615940e+02
    5.67537804e+00 -4.35088906e+02 -9.76974016e+01  7.71909591e+01
   -3.88738749e+02 -6.29099586e+01 -2.99970496e+01 -2.25985794e+02
    6.41590789e+01  1.03847001e+02 -7.32419021e+01  1.04802558e+02
    1.26585822e+00 -1.20170579e+02 -2.82526049e+01  1.57900698e+00
   -1.51780249e+02 -3.52080548e+01  8.84543993e-01 -1.07795356e+02
   -2.56307189e+01  8.84543993e-01 -1.07795356e+02 -2.56307189e+01
    5.67537804e+00 -4.35088906e+02 -9.76974016e+01  8.01141013e+00
   -4.16078607e+02 -1.25355227e+02  1.17740492e+00 -2.55151916e+02
   -7.20503620e+01 -1.73992688e+01 -2.44854505e+02 -9.25408725e+01
    8.70569014e-01 -1.68664569e+02 -3.73902498e+01  1.39982512e+00
   -2.00884252e+02 -4.47207875e+01  5.24591115e-01 -1.65867774e+02
   -3.68342864e+01  5.24591115e-01 -1.65867774e+02 -3.68342864e+01]]]

It leads to , an array [x0,y0,z0,x1,y1,z1,x3,y3,z3...... x31,y31,z31] and i want to extract the following ,

x = [x0, x2, .......... x31]
y= [y0, y1, ...........y31]
z= [z0,z1,..............z31]

I was wondering if thats possible to do it in the list or modifying array

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
tim tensor
  • 39
  • 8

2 Answers2

0

Edit:

As suggested in subsampling every nth entry in a numpy array , Numpy.ndarray offers a simple and elegant option:

import numpy as np
arr = np.array(my_list)
x = arr[::3]
y = arr[1::3]
z = arr[2::3]

Original answer:

I'm not sure about the structure of your input list, as what you provided here is an invalid data.

Despite the extra brackets in your source data, this can be done with a fairly simple trick, and you don't have to be bothered with Numpy:

Assuming that you are trying to somehow split the list into three vectors in your specified way, assign your list to a, then you could have your desired lists by:

x = [a[3*i] for i in range(int(len(a)/3))]
y = [a[3*i+1] for i in range(int(len(a)/3))]
z = [a[3*i+2] for i in range(int(len(a)/3))]

Shawn. L
  • 403
  • 1
  • 3
  • 13
  • The problem with this is that it'd be way slower than numpy indexing. – rafaelc May 09 '19 at 15:18
  • That is true. Compared to looping through the whole list three times, Numpy should have faster implementations when performing indexing. Yet it also depends on the size of the arr. In this case, an arr of 96 does not have too much difference though. – Shawn. L May 09 '19 at 15:22
  • Sorry, it was my mistake it was`1x96` array, of the order `x0,y0,z0, x1,x2,x3,.... x31,y31,z31]` and i want to achieve something `x_fin = [x0,x1,x2,x3]` . I guess on further look, i have to use `array_slciing` ? – tim tensor May 09 '19 at 16:13
  • I've updated my answer and it should work in your case. – Shawn. L May 09 '19 at 17:40
  • It seems slicing offers an option with more simplicity and elegance. After initializing the list into Numpy.ndarray, you could achieve this by ```x=arr[::3], y=arr[1::3], z=arr[2::3]``` – Shawn. L May 09 '19 at 17:50
0

IICU, given an array

[x0,y0,z0,x1,y1,z1,x2,y2,z2, .... , x31,y31,z31] 

You can just use numpy indexing

x = arr[np.arange(0,len(arr),3)]
y = arr[np.arange(1,len(arr),3)]
z = arr[np.arange(2,len(arr),3)]
rafaelc
  • 57,686
  • 15
  • 58
  • 82
  • Hi, sorry i had made a mistake, i had a `1x96` array , which is of the form `[x0,y0,z0,x1,y1,z1..... x31,y31,z31]` and arrange them [`x1,x2,x3 ]`so every third element of original matrix corresponds to x . Is np.arrange the right way ? – tim tensor May 09 '19 at 16:01