嵌套列表转换为python中的一个列表

时间:2018-12-26 13:16:58

标签: python python-3.x list

[[1755], [1126], [1098], [1618], [1618], [852], [1427], [1044], [852], [1755], [1718], [819], [1323], [1961], [1113], [1126], [1413], [1658], [1718], [1718], [1035], [1618], [1618]]

这是一个嵌套列表,每个项目都是一个列表,我想按如下所示制作此列表:

[1755, 1126, 1098, 1618, 1618,852, 1427, 1044, 852, 1755, 1718, 819, 1323, 1961, 1113, 1126, 1413, 1658, 1718, 1718, 1035, 1618, 1618]

9 个答案:

答案 0 :(得分:3)

对于最一般的情况,this主题已经具有所有答案。

在这种非常特殊的情况下,您可以使用x, = [foo]惯用法来解开长度为1的可迭代对象。

>>> lst = [[1755], [1126], [1098], [1618]]
>>> [x for x, in lst]                                                              
[1755, 1126, 1098, 1618]

答案 1 :(得分:1)

如果每个子列表只有一个元素,这是一种解决方案:

tmp = []
for sublist in list:
  tmp.append(sublist[0])

另一个选择:

flat_list = [sublist[0] for sublist in list]

如果此解决方案适合您,请立即投票+关闭。

答案 2 :(得分:1)

l= [[1755], [1126], [1098], [1618], [1618], [852], [1427], [1044], [852], [1755], [1718], [819], [1323], [1961], [1113], [1126], [1413], [1658], [1718], [1718], [1035], [1618], [1618]]

flat_list = [item for sublist in l for item in sublist]
flat_list
    [1755, 1126, 1098, 1618, 1618, 852, 1427, 1044, 852, 1755, 1718, 819, 1323,
     1961, 1113, 1126, 1413, 1658, 1718, 1718, 1035, 1618, 1618]

答案 3 :(得分:1)

您可以使用itertools.chain(*iterables)

In [316]: from itertools import chain
In [315]: l = [[1755], [1126], [1098], [1618], [1618], [852], [1427], [1044], [852], [1755], [1718], [819], [1323], [1961], [1113], [1126], [14
     ...: 13], [1658], [1718], [1718], [1035], [1618], [1618]]

In [317]: list(chain(*l))
Out[317]: 
[1755,
 1126,
 1098,
 1618,
 1618,
 852,
 1427,
 1044,
 852,
 1755,
 1718,
 819,
 1323,
 1961,
 1113,
 1126,
 1413,
 1658,
 1718,
 1718,
 1035,
 1618,
 1618]

答案 4 :(得分:1)

我想,您可以使用extend。这将适用于任何大小的子列表。

main_list = [[1755], [1126], [1098], [1618]]
resultant_list = []
for subpart in main_list:
    resultant_list.extend(subpart)

enter image description here

答案 5 :(得分:0)

尝试一下:

testlist = [[1755], [1126], [1098]]
ans = [e for e[0] in testlist]

答案 6 :(得分:0)

这是使用reduce

的另一种解决方案
from functools import reduce

list(reduce(lambda x, y: x + y, lst))

[1755,
 1126,
 1098,
 1618,
 1618,
 852,
 1427,
 1044,
 852,
 1755,
 1718,
 819,
 1323,
 1961,
 1113,
 1126,
 1413,
 1658,
 1718,
 1718,
 1035,
 1618,
 1618]

答案 7 :(得分:0)

也许您可以定义一个自定义方法,该方法也可以按级别进行展平:

def flatten(lst, level=1):
  res = []
  for item in lst:
    if isinstance(item, list):
      for subitem in item: res.append(subitem)
    else: res.append(item)
  if level == 1: return res
  else: return flatten(res, level-1)

因此您可以通过这种方式使用它,例如:

lst = [1,[2,[3,[4,[5]]]]]

print(flatten(lst))   #=> [1, 2, [3, [4, [5]]]]
print(flatten(lst,2)) #=> [1, 2, 3, [4, [5]]]
print(flatten(lst,3)) #=> [1, 2, 3, 4, [5]]
print(flatten(lst,4)) #=> [1, 2, 3, 4, 5]

在您的情况下,只需:

l = [[1755], [1126], [1098], [1618], [1618], [852], [1427], [1044], [852], [1755], [1718], [819], [1323], [1961], [1113], [1126], [1413], [1658], [1718], [1718], [1035], [1618], [1618]]
print(flatten(l))
#=> [1755, 1126, 1098, 1618, 1618, 852, 1427, 1044, 852, 1755, 1718, 819, 1323, 1961, 1113, 1126, 1413, 1658, 1718, 1718, 1035, 1618, 1618]

答案 8 :(得分:0)

这是解决方案。

nested_list=[[1755], [1126], [1098], [1618], [1618], [852], [1427], [1044], [852], [1755], [1718], [819], [1323], [1961], [1113], [1126], [1413], [1658], [1718], [1718], [1035], [1618], [1618]]

new_list=[]
for i in nested_list:
    current_element=i[0]
    new_list.append(current_element)
print(new_list)

这是一个非常适合初学者的解决方案

相关问题