Nested list transform to one list in python
本问题已经有最佳答案,请猛点这里访问。
1 | [[1755], [1126], [1098], [1618], [1618], [852], [1427], [1044], [852], [1755], [1718], [819], [1323], [1961], [1113], [1126], [1413], [1658], [1718], [1718], [1035], [1618], [1618]] |
这是嵌套列表,每个项目都是一个列表,我想让这个列表如下所示:
1 | [1755, 1126, 1098, 1618, 1618,852, 1427, 1044, 852, 1755, 1718, 819, 1323, 1961, 1113, 1126, 1413, 1658, 1718, 1718, 1035, 1618, 1618] |
对于最一般的情况,这个主题已经有了所有的答案。
在这个非常具体的例子中,您可以使用
1 2 3 | >>> lst = [[1755], [1126], [1098], [1618]] >>> [x for x, in lst] [1755, 1126, 1098, 1618] |
我想,你可以用
1 2 3 4 | main_list = [[1755], [1126], [1098], [1618]] resultant_list = [] for subpart in main_list: resultant_list.extend(subpart) |
您可以使用
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 | 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] |
1 2 3 4 5 6 | 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] |
如果每个子列表只有一个元素,那么下面是一个解决方案:
1 2 3 | tmp = [] for sublist in list: tmp.append(sublist[0]) |
另一种选择:
1 | flat_list = [sublist[0] for sublist in list] |
如果此解决方案适合您的需要,请毫不犹豫地向上投票+关闭。
这是解决方案。
1 2 3 4 5 6 7 | 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) |
这是一个非常适合初学者的解决方案
也许您可以定义一个自定义方法,它也可以按级别展开:
1 2 3 4 5 6 7 8 | 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) |
因此,您可以这样使用它,例如:
1 2 3 4 5 6 | 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] |
在您的情况下,只需:
1 2 3 | 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] |
这是另一个使用
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 | 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] |
试试这个:
1 2 | testlist = [[1755], [1126], [1098]] ans = [e for e[0] in testlist] |