转换并连接多个嵌套的列表列表

时间:2017-02-23 16:37:40

标签: python list nested

我有以下嵌套的列表列表:

a = [[[[-79.43402638260521, -1.69184588855758], [-79.4339722432865, -1.691845844583909], [-79.43397178076256, -1.691851284533779],
    [-79.43395283944169, -1.692053292637794], [-79.43395281911414, -1.692054736321033], [-79.43395535750368, -1.692093535418117],
    [-79.43390444734398, -1.69223087834723], [-79.43390428016939, -1.692231372437897], [-79.43374523144152, -1.692750043925838],
    [-79.4340256570161, -1.692750271834557], [-79.43402638260521, -1.69184588855758]]], [[[-79.43381375958064, -1.691845715849684],
    [-79.43312765678151, -1.691845158387183], [-79.4331269307764, -1.692749541273626], [-79.43354270912953, -1.692749879305633],
    [-79.43364983051107, -1.692588468489809], [-79.4336510738479, -1.692585646334773], [-79.43371548446397, -1.692327269168548],
    [-79.43380554258165, -1.692094789340216], [-79.43380615195998, -1.692091785860122], [-79.43381375958064, -1.691845715849684]]]]

我将如何从那里开始:

a = [[-79.43402638260521, -1.69184588855758], [-79.4339722432865, -1.691845844583909], [-79.43397178076256, -1.691851284533779], 
[-79.43395283944169, -1.692053292637794], [-79.43395281911414, -1.692054736321033], [-79.43395535750368, -1.692093535418117], 
[-79.43390444734398, -1.69223087834723], [-79.43390428016939, -1.692231372437897], [-79.43374523144152, -1.692750043925838], 
[-79.4340256570161, -1.692750271834557], [-79.43402638260521, -1.69184588855758], [-79.43381375958064, -1.691845715849684], 
[-79.43312765678151, -1.691845158387183], [-79.4331269307764, -1.692749541273626], [-79.43354270912953, -1.692749879305633], 
[-79.43364983051107, -1.692588468489809], [-79.4336510738479, -1.692585646334773], [-79.43371548446397, -1.692327269168548], 
[-79.43380554258165, -1.692094789340216], [-79.43380615195998, -1.692091785860122], [-79.43381375958064, -1.691845715849684]]

所以基本上删除嵌套列表和每个列表的开头和结尾的双括号。我尝试了以下和ittertools没有成功:

flatten = lambda list: [item for sublist in list for item in sublist]

注意:len(a)== 2

非常感谢!

5 个答案:

答案 0 :(得分:2)

假设你的列表不是任意嵌套的,但你只是试图更深层次,你可以选择一些非常简单的东西,如

android:src="@mipmap/matchstick.png"

<强>演示

a = [item for sublist in a for subsublist in sublist for item in subsublist]

然而正如你所说,在你的例子中你有一个空的嵌套级别,在这种情况下,可以很好地清理解决方案as they have shown

答案 1 :(得分:2)

使用itertools.chain.from_iterable

list(itertools.chain.from_iterable(xs[0] for xs in a))

上面类似于:a[0][0] + a[1][0] + ...(没有连接,导致列表创建在其中)

>>> a = [[[[-79.43402638260521, -1.69184588855758],
...        [-79.4339722432865, -1.691845844583909],
...        [-79.43397178076256, -1.691851284533779],
...        [-79.43395283944169, -1.692053292637794],
...        [-79.43395281911414, -1.692054736321033],
...        [-79.43395535750368, -1.692093535418117],
...        [-79.43390444734398, -1.69223087834723],
...        [-79.43390428016939, -1.692231372437897],
...        [-79.43374523144152, -1.692750043925838],
...        [-79.4340256570161, -1.692750271834557],
...        [-79.43402638260521, -1.69184588855758]]],
...      [[[-79.43381375958064, -1.691845715849684],
...        [-79.43312765678151, -1.691845158387183],
...        [-79.4331269307764, -1.692749541273626],
...        [-79.43354270912953, -1.692749879305633],
...        [-79.43364983051107, -1.692588468489809],
...        [-79.4336510738479, -1.692585646334773],
...        [-79.43371548446397, -1.692327269168548],
...        [-79.43380554258165, -1.692094789340216],
...        [-79.43380615195998, -1.692091785860122],
...        [-79.43381375958064, -1.691845715849684]]]]
>>> 
>>> import itertools
>>> import pprint
>>> b = list(itertools.chain.from_iterable(xs[0] for xs in a))
>>> pprint.pprint(b)

结果:

[[-79.43402638260521, -1.69184588855758],
 [-79.4339722432865, -1.691845844583909],
 [-79.43397178076256, -1.691851284533779],
 [-79.43395283944169, -1.692053292637794],
 [-79.43395281911414, -1.692054736321033],
 [-79.43395535750368, -1.692093535418117],
 [-79.43390444734398, -1.69223087834723],
 [-79.43390428016939, -1.692231372437897],
 [-79.43374523144152, -1.692750043925838],
 [-79.4340256570161, -1.692750271834557],
 [-79.43402638260521, -1.69184588855758],
 [-79.43381375958064, -1.691845715849684],
 [-79.43312765678151, -1.691845158387183],
 [-79.4331269307764, -1.692749541273626],
 [-79.43354270912953, -1.692749879305633],
 [-79.43364983051107, -1.692588468489809],
 [-79.4336510738479, -1.692585646334773],
 [-79.43371548446397, -1.692327269168548],
 [-79.43380554258165, -1.692094789340216],
 [-79.43380615195998, -1.692091785860122],
 [-79.43381375958064, -1.691845715849684]]

答案 2 :(得分:2)

这样做了。

a = a[0][0] + a[1][0]

这可以扩展为,

a = sum([a[i][0] for i in range(len(a))], [])

答案 3 :(得分:0)

您可以尝试:

[z for x in a for y in x for z in y]

并证明:

len([y for x in a for y in x]) == 21

您可以使用pprint

正确查看
pprint.pprint(a)

输出继电器:

[[[[-79.43402638260521, -1.69184588855758],
   [-79.4339722432865, -1.691845844583909],
   [-79.43397178076256, -1.691851284533779],
   [-79.43395283944169, -1.692053292637794],
   [-79.43395281911414, -1.692054736321033],
   [-79.43395535750368, -1.692093535418117],
   [-79.43390444734398, -1.69223087834723],
   [-79.43390428016939, -1.692231372437897],
   [-79.43374523144152, -1.692750043925838],
   [-79.4340256570161, -1.692750271834557],
   [-79.43402638260521, -1.69184588855758]]],
 [[[-79.43381375958064, -1.691845715849684],
   [-79.43312765678151, -1.691845158387183],
   [-79.4331269307764, -1.692749541273626],
   [-79.43354270912953, -1.692749879305633],
   [-79.43364983051107, -1.692588468489809],
   [-79.4336510738479, -1.692585646334773],
   [-79.43371548446397, -1.692327269168548],
   [-79.43380554258165, -1.692094789340216],
   [-79.43380615195998, -1.692091785860122],
   [-79.43381375958064, -1.691845715849684]]]]

pprint.pprint([z for x in a for y in x for z in y])

输出:

[[-79.43402638260521, -1.69184588855758],
 [-79.4339722432865, -1.691845844583909],
 [-79.43397178076256, -1.691851284533779],
 [-79.43395283944169, -1.692053292637794],
 [-79.43395281911414, -1.692054736321033],
 [-79.43395535750368, -1.692093535418117],
 [-79.43390444734398, -1.69223087834723],
 [-79.43390428016939, -1.692231372437897],
 [-79.43374523144152, -1.692750043925838],
 [-79.4340256570161, -1.692750271834557],
 [-79.43402638260521, -1.69184588855758],
 [-79.43381375958064, -1.691845715849684],
 [-79.43312765678151, -1.691845158387183],
 [-79.4331269307764, -1.692749541273626],
 [-79.43354270912953, -1.692749879305633],
 [-79.43364983051107, -1.692588468489809],
 [-79.4336510738479, -1.692585646334773],
 [-79.43371548446397, -1.692327269168548],
 [-79.43380554258165, -1.692094789340216],
 [-79.43380615195998, -1.692091785860122],
 [-79.43381375958064, -1.691845715849684]]

答案 4 :(得分:0)

您需要迭代嵌套列表并加入它们。

一种简单的方法:

def flatten1(list_of_lists):
  "Flattens one level of lists."
  result = []
  for sub_list in list_of_lists:
    result.extend(sub_list)
  return result

一种聪明的方式,利用您可以添加列表的事实:

flatten1 = lambda(list_of_lists): sum(list_of_lists, [])

现在,您可以a[0] = flatten1(a[0])