将元组列表的列表重塑为NumPy数组

时间:2018-09-03 09:34:17

标签: python arrays list numpy

我是python的新手,正面临一些看起来很简单的问题,但我无法弄清楚。

我有一个看起来像这样的坐标列表:

[(-80983.957,175470.593,393.486),(-80994.122,175469.889,394.391),(-80996.591,175469.757,394.595),(-81006.72,175469.298,395.527),(-81019.538,175468.399,396.847) -81025.594、175467.643、397.395),(-81030.366、175467.25、397.858),(-81030.455、175467.776、397.886),(-81033.209、175470.89、398.192)],     [(-81037.166,175471.423,398.729),(-81037.911,175471.237,398.764),(-81038.01,175472.285,398.533)],     [(-81030.537,175472.562,397.951),(-81029.792,175472.896,397.842),(-81029.668,175472.131,397.895)],     [(-80847.281,175417.644,378.2),(--80853.651,175417.621,378.781)]

,我想将其转换为形状为(:,3)的数组,以便每行包含一组x,y,z坐标。

我尝试了np.asarray的基础知识,但是它只返回了最后一个[(,,),(,,)]“段落”的元素,因此:

[(-80847.281, 175417.644, 378.2), (-80853.651, 175417.621, 378.781)]

我还阅读了有关嵌套列表的信息,并尝试了几种使列表变平的技术,但这给了我同样的失败结果。

我想要一个包含所有这些“段落”的数组。你有一个想法怎么做?为什么只返回最后一个“段落”?

我没有找到任何等效的列表形状(如果可以帮助我使用dxfgrabber函数从dxf文件中获得形状)。

4 个答案:

答案 0 :(得分:1)

如果我的理解正确,则需要一个形状为(n,3)的数组,即长度为n的坐标为3的数组。您可以使用列表理解和np.array构造函数来做到这一点:

import numpy as np
l = [[(-80983.957, 175470.593, 393.486), (-80994.122, 175469.889, 394.391), (-80996.591, 175469.757, 394.595), (-81006.72, 175469.298, 395.527), (-81019.538, 175468.399, 396.847), (-81025.594, 175467.643, 397.395), (-81030.366, 175467.25, 397.858), (-81030.455, 175467.776, 397.886), (-81033.209, 175470.89, 398.192)],
[(-81037.166, 175471.423, 398.729), (-81037.911, 175471.237, 398.764), (-81038.01, 175472.285, 398.533)],
[(-81030.537, 175472.562, 397.951), (-81029.792, 175472.896, 397.842), (-81029.668, 175472.131, 397.895)],
[(-80847.281, 175417.644, 378.2), (-80853.651, 175417.621, 378.781)]]
a = np.array([list(t) for ll in l for t in ll])

a设为:

array([[ -80983.957,  175470.593,     393.486],
       [ -80994.122,  175469.889,     394.391],
       [ -80996.591,  175469.757,     394.595],
       [ -81006.72 ,  175469.298,     395.527],
       [ -81019.538,  175468.399,     396.847],
       [ -81025.594,  175467.643,     397.395],
       [ -81030.366,  175467.25 ,     397.858],
       [ -81030.455,  175467.776,     397.886],
       [ -81033.209,  175470.89 ,     398.192],
       [ -81037.166,  175471.423,     398.729],
       [ -81037.911,  175471.237,     398.764],
       [ -81038.01 ,  175472.285,     398.533],
       [ -81030.537,  175472.562,     397.951],
       [ -81029.792,  175472.896,     397.842],
       [ -81029.668,  175472.131,     397.895],
       [ -80847.281,  175417.644,     378.2  ],
       [ -80853.651,  175417.621,     378.781]])

答案 1 :(得分:1)

给出具有指定结构的列表输入L,您可以展平列表列表并将其馈送到np.array

import numpy as np
from itertools import chain

res = np.array(list(chain.from_iterable(L)))

print(res)

array([[ -80983.957,  175470.593,     393.486],
       [ -80994.122,  175469.889,     394.391],
       [ -80996.591,  175469.757,     394.595],
       ...
       [ -81029.668,  175472.131,     397.895],
       [ -80847.281,  175417.644,     378.2  ],
       [ -80853.651,  175417.621,     378.781]])

答案 2 :(得分:1)

如果我将您的样本复制-n-粘贴到python会话中,则会得到一个包含4个列表的元组:

In [685]: [(-80983.957, 175470.593, 393.486), (-80994.122, 175469.889, 394.391), (-80996.591, 1754
     ...: 69.757, 394.595), (-81006.72, 175469.298, 395.527), (-81019.538, 175468.399, 396.847), (
     ...: -81025.594, 175467.643, 397.395), (-81030.366, 175467.25, 397.858), (-81030.455, 175467.
     ...: 776, 397.886), (-81033.209, 175470.89, 398.192)], [(-81037.166, 175471.423, 398.729), (-
     ...: 81037.911, 175471.237, 398.764), (-81038.01, 175472.285, 398.533)], [(-81030.537, 175472
     ...: .562, 397.951), (-81029.792, 175472.896, 397.842), (-81029.668, 175472.131, 397.895)], [
     ...: (-80847.281, 175417.644, 378.2), (-80853.651, 175417.621, 378.781)]
Out[685]: 
([(-80983.957, 175470.593, 393.486),
  (-80994.122, 175469.889, 394.391),
  (-80996.591, 175469.757, 394.595),
  (-81006.72, 175469.298, 395.527),
  (-81019.538, 175468.399, 396.847),
  (-81025.594, 175467.643, 397.395),
  (-81030.366, 175467.25, 397.858),
  (-81030.455, 175467.776, 397.886),
  (-81033.209, 175470.89, 398.192)],
 [(-81037.166, 175471.423, 398.729),
  (-81037.911, 175471.237, 398.764),
  (-81038.01, 175472.285, 398.533)],
 [(-81030.537, 175472.562, 397.951),
  (-81029.792, 175472.896, 397.842),
  (-81029.668, 175472.131, 397.895)],
 [(-80847.281, 175417.644, 378.2), (-80853.651, 175417.621, 378.781)])

np.vstack很好地将其转换为二维数组:

In [686]: np.vstack(_)
Out[686]: 
array([[-80983.957, 175470.593,    393.486],
       [-80994.122, 175469.889,    394.391],
       [-80996.591, 175469.757,    394.595],
       [-81006.72 , 175469.298,    395.527],
       [-81019.538, 175468.399,    396.847],
       [-81025.594, 175467.643,    397.395],
       [-81030.366, 175467.25 ,    397.858],
       [-81030.455, 175467.776,    397.886],
       [-81033.209, 175470.89 ,    398.192],
       [-81037.166, 175471.423,    398.729],
       [-81037.911, 175471.237,    398.764],
       [-81038.01 , 175472.285,    398.533],
       [-81030.537, 175472.562,    397.951],
       [-81029.792, 175472.896,    397.842],
       [-81029.668, 175472.131,    397.895],
       [-80847.281, 175417.644,    378.2  ],
       [-80853.651, 175417.621,    378.781]])

一些解释

在大多数情况下,用逗号分隔python对象会生成一个元组(即使没有()`:

In [687]: x = [1,2,3],[4,5,6,6]
In [688]: x
Out[688]: ([1, 2, 3], [4, 5, 6, 6])

vstack遍历输入列表或元组,并将每个元素转换为2d数组。然后将它们沿第一轴连接。

In [690]: [np.atleast_2d(i) for i in x]
Out[690]: [array([[1, 2, 3]]), array([[4, 5, 6, 6]])]

在这种情况下,连接不起作用,因为列数不匹配;在您的4个列表中包含多个3个元素元组,因此成为(n,3)个数组。

答案 3 :(得分:0)

您是否真的想弄平您的列表?

import itertools

flattened_list = list(itertools.chain.from_iterable(original_list))