如何将numpy数组转换为嵌套列表?

时间:2016-11-02 05:53:49

标签: python arrays list numpy

我得到了一个numpy数组:

[[  260586.25967407  2736302.81560516   260586.25967407  2736273.93140412]
 [  260586.25967407  2736302.81560516   260615.14387512  2736302.81560516]
 [  252209.84136963  2744072.66568756   252238.72557068  2744072.66568756]
 [  252209.84136963  2744072.66568756   252209.84136963  2744043.78148651]
 [  263041.41676331  2727262.06067658   263041.41676331  2727233.17647553]
 [  263041.41676331  2727262.06067658   263070.30096436  2727262.06067658]
 [  270782.38264465  2733241.09029389   270753.4984436   2733269.97449494]
 [  270782.38264465  2733241.09029389   270782.38264465  2733212.20609284]]

我希望将其转换为嵌套列表,如下所示(嵌套中有2行):

[[((260586.25967407227, 2736302.815605165), (260586.25967407227, 2736273.931404115)), ((260586.25967407227, 2736302.815605165), (260615.1438751221, 2736302.815605165))], [((252209.84136962818, 2744072.6656875624), (252238.72557067798, 2744072.6656875624)),  ((252209.84136962818, 2744072.6656875624), (252209.84136962818, 2744043.7814865126))], [((263041.4167633059, 2727262.060676576), (263041.4167633059, 2727233.1764755263)), ((263041.4167633059, 2727262.060676576), (263070.3009643557, 2727262.060676576))], [((270782.38264465425, 2733241.0902938857), (270753.49844360445, 2733269.9744949355)), ((270782.38264465425, 2733241.0902938857), (270782.38264465425, 2733212.206092836))]]`

我尝试了以下代码,但没有得到我想要的内容:

abc = numpy.genfromtxt('abc.csv', delimiter = ',')
b = abc.tolist()
print b
[[260586.25967407227, 2736302.815605165, 260586.25967407227, 2736273.931404115], [260586.25967407227, 2736302.815605165, 260615.1438751221, 2736302.815605165], [252209.84136962818, 2744072.6656875624, 252238.72557067798, 2744072.6656875624], [252209.84136962818, 2744072.6656875624, 252209.84136962818, 2744043.7814865126], [263041.4167633059, 2727262.060676576, 263041.4167633059, 2727233.1764755263], [263041.4167633059, 2727262.060676576, 263070.3009643557, 2727262.060676576], [270782.38264465425, 2733241.0902938857, 270753.49844360445, 2733269.9744949355], [270782.38264465425, 2733241.0902938857, 270782.38264465425, 2733212.206092836]]

3 个答案:

答案 0 :(得分:0)

您几乎就在那里,您只需要将b转换为所需的结果。有了comperhension

convert = lambda x: (tuple(x[:2]), tuple(x[2:4]))
end_data = [[convert(b[i]), convert(b[i+1])] for i in range(0, len(b), 2)]

In [43]: end_data
Out[43]:
[[((260586.25967407227, 2736302.815605165),
   (260586.25967407227, 2736273.931404115)),
  ((260586.25967407227, 2736302.815605165),
   (260615.1438751221, 2736302.815605165))],
 [((252209.84136962818, 2744072.6656875624),
   (252238.72557067798, 2744072.6656875624)),
  ((252209.84136962818, 2744072.6656875624),
   (252209.84136962818, 2744043.7814865126))],
 [((263041.4167633059, 2727262.060676576),
   (263041.4167633059, 2727233.1764755263)),
  ((263041.4167633059, 2727262.060676576),
   (263070.3009643557, 2727262.060676576))],
 [((270782.38264465425, 2733241.0902938857),
   (270753.49844360445, 2733269.9744949355)),
  ((270782.38264465425, 2733241.0902938857),
   (270782.38264465425, 2733212.206092836))]]

答案 1 :(得分:0)

这是使用reshapingmapping to tuple -

的一种方法
[map(tuple,i) for i in a.reshape(-1,a.shape[1],2)]

示例运行 -

In [144]: a
Out[144]: 
array([[ 0.11320329,  0.38631274,  0.16489868,  0.14579492],
       [ 0.98843975,  0.28702951,  0.43839608,  0.30787642],
       [ 0.89358306,  0.29894411,  0.87855629,  0.34827855],
       [ 0.91250334,  0.57001322,  0.31693489,  0.89326656],
       [ 0.7653153 ,  0.59759463,  0.32264769,  0.62004214],
       [ 0.56097733,  0.28806541,  0.3077928 ,  0.80165235],
       [ 0.6463753 ,  0.56006258,  0.19069942,  0.40113307],
       [ 0.12401688,  0.67079581,  0.11539712,  0.01450834]])

In [145]: [map(tuple,i) for i in a.reshape(-1,a.shape[1],2)]
Out[145]: 
[[(0.11320329242532168, 0.38631274137824767),
  (0.16489867576692918, 0.14579492208404965),
  (0.98843974945147983, 0.28702950772300462),
  (0.43839607607462827, 0.30787642048153419)],
 [(0.89358305668503202, 0.2989441055450931),
  (0.87855629360209708, 0.34827855409176289),
  (0.91250334294313873, 0.57001322288255962),
  (0.31693488736085818, 0.89326656458019482)],
 [(0.76531530133455727, 0.59759462688278808),
  (0.32264769282936523, 0.62004214421467019),
  (0.5609773331562572, 0.28806541448767242),
  (0.30779280383830754, 0.80165235436491056)],
 [(0.64637529964418394, 0.56006257664684178),
  (0.19069942156215225, 0.40113306863414666),
  (0.12401688191416316, 0.67079581116123166),p
  (0.11539712246106293, 0.014508339381489432)]]

答案 2 :(得分:0)

  1. 您必须将每个内部列表从形状(4,)重塑为形状(2,2)。
  2. 将每个重新整形的数组转换为元组。这可以使用if (person instanceof Student) student = (Student) person;
  3. 完成
  4. 需要再次将地图对象转换为元组。
  5. a 中的每个条目重复步骤1到3。
  6. 将所有内容放入嵌套列表中。
  7. 该过程可归纳为以下一行:

    map