将列表转换为数组,并从转换后的数组中提取特定坐标

时间:2019-05-09 14:56:01

标签: python arrays numpy numpy-ndarray

我有一个输出作为列表,需要在上面执行一些操作。为此,我使用np.asarray function转换为数组。

我的列表

[[ 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]])]

我计划提取第一列,并创建vector x提取第二列vector y和第三列vector z。但是,当我将其转换为数组时,它变成具有以下结构的1x1x96数组类型

[[[ 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]]]

它导致一个数组[x0,y0,z0,x1,y1,z1,x3,y3,z3...... x31,y31,z31],我想提取以下内容

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

我想知道是否可以在list中进行修改或修改array

2 个答案:

答案 0 :(得分:0)

编辑:

根据subsampling every nth entry in a numpy array 中的建议,Numpy.ndarray提供了一个简单而优雅的选择:

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

原始答案:

我不确定您的输入列表的结构,因为您在此处提供的是无效数据。

尽管源数据中有多余的括号,这可以通过一个非常简单的技巧来完成,并且不必为Numpy所困扰:

假设您正尝试以某种方式将列表分为三个向量,请将列表分配给a,则可以通过以下方式获得所需的列表:

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))]

答案 1 :(得分:0)

IICU,有一个数组

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

您可以只使用numpy索引

x = arr[np.arange(0,len(arr),3)]
y = arr[np.arange(1,len(arr),3)]
z = arr[np.arange(2,len(arr),3)]