[英]How can I convert a list with sublist into a uniform list (flattening)?
I have a list of coordinates as following:我有一个坐标列表如下:
temp = [((20.0, 15.076858380630263), (20.0, 16.92468707613784)), (5.430012747754155, 3.3503228946034667), (7.364023506893538, 7.013767290971433), (11.932318028181742, 8.766709807679579), ((12.839934501779176, 11.300824688102473), (13.285114114146213, 14.10378570292717),(14.839934501779176, 16.300824688102473))]
As you can see there some nested lists with multiple coordinates, so I want it be look like the following:正如您所看到的,有一些具有多个坐标的嵌套列表,所以我希望它如下所示:
temp =[(20.0, 15.076858380630263), (20.0, 16.92468707613784), (5.430012747754155, 3.3503228946034667), (7.364023506893538, 7.013767290971433), (11.932318028181742, 8.766709807679579), (12.839934501779176, 11.300824688102473), (13.285114114146213, 14.10378570292717), (14.839934501779176, 16.300824688102473)]
This is just an example and I have a huge output and do not know where these nested listed located with how many coordinates inside it.这只是一个例子,我有一个巨大的输出,不知道这些嵌套列表位于哪里,里面有多少坐标。 But the overall structure is the same.但整体结构是一样的。
Thank you.谢谢你。
you can use a list comprehension:您可以使用列表理解:
[j for i in temp for j in (i if isinstance(i[0], tuple) else [i])]
output:输出:
[(20.0, 15.076858380630263),
(20.0, 16.92468707613784),
(5.430012747754155, 3.3503228946034667),
(7.364023506893538, 7.013767290971433),
(11.932318028181742, 8.766709807679579),
(12.839934501779176, 11.300824688102473),
(13.285114114146213, 14.10378570292717),
(14.839934501779176, 16.300824688102473)]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.