Python:从数组元组列表到列表列表

时间:2014-05-16 02:34:15

标签: arrays list python-2.7 numpy tuples

我不知道我是如何弄乱代码属性的,但我最终得到了一个怪物:一个列表,其元素是元组,其元素是数组......

[(array([ 0.00773887,  0.00531894,  0.00533349,  0.00779727,  0.01482933,
    0.01247594,  0.01274703,  0.02111097,  0.01800994,  0.01398229,
    0.0098171 ,  0.01218634,  0.00987849,  0.01082564,  0.00739867,
    0.00930694,  0.01071009,  0.00871869,  0.00897236,         nan,
    0.06841951,  0.03529371,         nan,  0.016373  ,  0.0160713 ,
    0.0182685 ,         nan,  0.01816914,  0.01156275,  0.01283952,
           nan,  0.00962016,  0.00861139,  0.00776996,  0.0324668 ,
    0.00745239]),), (array([ 0.0059394 ,  0.00634485,  0.00588112,  0.00583169,  0.01051727,
    0.01061778,  0.01368865,  0.01033937,  0.0105504 ,  0.01073401,
    0.01276537,  0.01121004,  0.00761677,  0.01370088,  0.01133099,
    0.01176184,  0.00922666,  0.00655782,  0.00608386,  0.00686759,
    0.00935311,  0.01204305,  0.00912691,  0.01046725,  0.01721009,
    0.01446536,  0.01320765,  0.01304908,  0.01170495,  0.00884054,
    0.00964988,  0.01170055,  0.00673198,  0.00543281,  0.00610345,
    0.0072238 ]),), ... (array([ 0.00839741,  0.01946217,  0.01032584,  0.00857666,  0.01208251,
    0.02800771,  0.02556111,  0.01417291,  0.01771353,  0.02820838,
    0.01647876,  0.02092841,  0.0193186 ,  0.03620055,  0.01485362,
    0.01272976,  0.01202848,  0.01717644,  0.0149781 ,  0.01955458,
    0.0221506 ,  0.0452485 ,  0.03533813,         nan,         nan,
           nan,  0.06147576,  0.02526941,  0.01906981,  0.02111215,
    0.0184051 ,  0.01271902,  0.00860239,  0.00950814,  0.01013899,
    0.00810949]),)]

如何转换元素以获取SIMPLE列表列表?

2 个答案:

答案 0 :(得分:3)

它可能不是最快的,但这应该非常强大:

lst = np.array(lst).tolist()

答案 1 :(得分:-1)

只需调用以下列表理解:

lst = [item.tolist() for item in lst]

运行如下:

>>> lst = [(array([ 0.00773887,  0.00531894,  0.00533349,  0.00779727,  0.01482933,
...     0.01247594,  0.01274703,  0.02111097,  0.01800994,  0.01398229,
...     0.0098171 ,  0.01218634,  0.00987849,  0.01082564,  0.00739867,
...     0.00930694,  0.01071009,  0.00871869,  0.00897236,         nan,
...     0.06841951,  0.03529371,         nan,  0.016373  ,  0.0160713 ,
...     0.0182685 ,         nan,  0.01816914,  0.01156275,  0.01283952,
...            nan,  0.00962016,  0.00861139,  0.00776996,  0.0324668 ,
...     0.00745239]))]
>>> 
>>> lst = [item.tolist() for item in lst]
>>> 
>>> lst
[[0.00773887, 0.00531894, 0.00533349, 0.00779727, 0.01482933, 0.01247594, 0.01274703, 0.02111097, 0.01800994, 0.01398229, 0.0098171, 0.01218634, 0.00987849, 0.01082564, 0.00739867, 0.00930694, 0.01071009, 0.00871869, 0.00897236, nan, 0.06841951, 0.03529371, nan, 0.016373, 0.0160713, 0.0182685, nan, 0.01816914, 0.01156275, 0.01283952, nan, 0.00962016, 0.00861139, 0.00776996, 0.0324668, 0.00745239]]
>>>