Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Access element of list of arrays

I have a list of data made up of arrays. I want to able to access each element of these arrays. However, when I try to access each element of one of the lists I instead get a whole list returned i.e.

>>> vec_data[0]
'[array([129.1203,  65.5152, -15.7962]), array([ -75.8793,  -27.2767, -136.3468]), array([  98.54  , -110.7603,  187.9171]), array([ 83.9628, 126.839 , -61.4756]), array([ -63.1397, -216.348 , -165.1392]), array([ 32.8174,  45.1573, 146.6547]), array([65.2439, 82.8289, 13.3427]), array([  -8.481 ,  -38.3387, -138.1786]), array([-223.8824,   86.1375,   -2.5446]), array([211.8308,  51.894 ,  89.5078]), array([  21.4793, -177.2219,   -0.573 ])]\n'

I then tried vec_data[0][0] hoping to get [129.1203, 65.5152, -15.7962] returned but instead got:

>>> vec_data[0][0]
'['

Can anyone provide any insight on how I may get back the output I want from this list?

I have attached a small excerpt of the data below to aid the problem.

['[array([129.1203,  65.5152, -15.7962]), array([ 83.094 , 135.5689,  57.7153]), array([  97.337 , -158.2453,   26.0811]), array([  31.3163,  121.7492, -100.4891]), array([ 60.797 ,  20.4243, -45.3821]), array([  -1.9805, -145.0236,  -57.6177]), array([116.4612,  18.3822, -99.8011]), array([38.402 , 13.7894, 85.094 ]), array([-321.057 ,  147.0128,  173.035 ]), array([  99.6488,  -74.9626, -110.7028]), array([ 134.3792, -110.4547,   -8.8729])]\n', '[array([ -67.876  ,  -28.46764, -118.16426]), array([ 58.368  , 107.64964,  90.12756]), array([ 102.6007, -165.1867,   26.1187]), array([  39.58716,  124.4238 , -106.68995]), array([ 58.19804,  18.7423 , -40.15905]), array([  -4.9479, -151.5177,  -56.3511]), array([ 99.946 ,  27.8355, -89.1489]), array([51.115, -3.302, 80.843]), array([-326.252 ,  170.7748,  168.0119]), array([ 120.9826,  -91.66  , -116.6699]), array([114.7974, -82.5404,   2.0438])]\n', '[array([ -28.196  ,  -14.69983, -149.5836 ]), array([ 54.399  , 108.75513, 106.2178 ]), array([  93.0723, -146.1217,   27.1268]), array([  15.1398,  104.9325, -128.3365]), array([ 89.2691,   2.3471, -21.0604]), array([ -15.2423, -155.4835,  -28.2395]), array([ 86.8051, -18.2533, -74.2126]), array([73.194 , 60.0037, 67.0507]), array([-314.68  ,  170.6532,  145.9454]), array([  69.4455,  -98.9357, -135.0111]), array([147.9465, -88.8198,  36.5976]

EDIT:

using the method suggested by revliscano:

import re
import json


string = vec_data[0]
r = re.sub('array\((\[.+?\])\)', lambda x: x.groups()[0], string)
r = r.replace('\n', '')
list_ = json.loads(r)

Give me the desired output for vec_data[0] however, I want to get this to loop over all of vec_data - any suggestions?

like image 706
raby Avatar asked Dec 01 '25 05:12

raby


1 Answers

If you have a way of not putting this data into your program as a string, that would be ideal, but I believe that the following should work to convert your data into a better format.

import numpy as np
array = np.array
vec_data = eval(vec_data)

After doing this with your sample code I got a list of arrays.

If you want this to be in a list format instead, you could replace array = np.array with array = list, but by the looks of your input data, it was probably originally a numpy array.

like image 127
duckboycool Avatar answered Dec 02 '25 18:12

duckboycool