Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/356.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在python中,如何在一个列表中组合每5个列表?_Python_List_Append_Iteration - Fatal编程技术网

在python中,如何在一个列表中组合每5个列表?

在python中,如何在一个列表中组合每5个列表?,python,list,append,iteration,Python,List,Append,Iteration,我有一个很大的列表,例如: list = [['a'], ['b'], ['c'], ['1234'], ['d'], ['e'], ['g'], ['h'], ['i'], ['56']] 我想把每5个列表合并成一个元素列表,以此类推。 我在一个列表中总共有150995个列表 预期产出: new_list = [['a' , 'b', 'c', '1234', 'd'], ['e', 'g', 'h', 'i', '56']] 我尝试了以下代码。但它是扁平化为一个列表 list(zip(*

我有一个很大的列表,例如:

list = [['a'], ['b'], ['c'], ['1234'], ['d'], ['e'], ['g'], ['h'], ['i'], ['56']]
我想把每5个列表合并成一个元素列表,以此类推。 我在一个列表中总共有150995个列表

预期产出:

new_list = [['a' , 'b', 'c', '1234', 'd'], ['e', 'g', 'h', 'i', '56']]
我尝试了以下代码。但它是扁平化为一个列表

list(zip(*list)[0])

我怎样才能做到这一点?

您基本上希望将大小均匀的列表块分组。可以使用
itertools.chain
并使用
range
和步骤5进行切片

import itertools

lst = [['a'], ['b'], ['c'], ['1234'], ['d'], ['e'], ['g'], ['h'], ['i'], ['56']]

result = [list(itertools.chain.from_iterable(itertools.islice(lst,i,i+5))) for i in range(0,len(lst),5)]


print(result)
结果:

[['a', 'b', 'c', '1234', 'd'], ['e', 'g', 'h', 'i', '56']]
注:

  • 使用
    itertools.islice
    可以避免标准的
    lst[i:i+5]
    切片,从而创建无用的
    列表
    对象
  • 即使元素数不能被5整除,它也能工作

使用
itertools.chain
将列表展平,并使用
itertools.zip\u longest
将元素分为5组

>>> from itertools import zip_longest, chain
>>> n = 5
>>> list(zip_longest(*[chain(*List)]*n))    
[('a', 'b', 'c', '1234', 'd'), ('e', 'g', 'h', 'i', '56')]
如果您对元组列表的结果不满意,可以使用
map

>>> list(map(list, zip_longest(*[chain(*List)]*5)))
[['a', 'b', 'c', '1234', 'd'], ['e', 'g', 'h', 'i', '56']]

此解决方案非常方便,因为它只使用
numpy

如果列表中的元素总是可以被5整除,那么

import numpy as np
oldlist = [['a'], ['b'], ['c'], ['1234'], ['d'], ['e'], ['g'], ['h'], ['i'], ['56']]
newlist = np.reshape(oldlist,(len(oldlist)//5,5)).T.tolist()
newlist将具有您所需的

[['a' , 'b', 'c', '1234', 'd'], ['e', 'g', 'h', 'i', '56']]
鉴于:

你可以做:

>>> list(map(list, zip(*[iter([e for sublist in li for e in sublist])]*5)))
[['a', 'b', 'c', '1234', 'd'], ['e', 'g', 'h', 'i', '56']]
或者


已经有很多好的解决方案。我只想再添加一种可能性,即使用自定义的
迭代器
,如下所示:

from itertools import chain


class MyIter:
    def __init__(self, lists,n_first):
        self.lists = lists
        self.index = 0
        self.n_first = n_first

    def __iter__(self):
        return self

    def __next__(self):
        if self.index < len(self.lists):

            temp = list(chain(*self.lists[self.index:min(self.index+self.n_first,len(self.lists))]))
            self.index += self.n_first
            return temp
        else:
            raise StopIteration

_list = [['a'], ['b'], ['c'], ['1234'], ['d'], ['e'], ['g'], ['h'], ['i'], ['56']]

print(list(MyIter(_list,5)))
来自itertools导入链的

密特级:
定义初始化(self,list,n_first):
self.lists=列表
self.index=0
self.n_first=n_first
定义(自我):
回归自我
定义下一个(自我):
如果self.index
假设
len(list)
是5的倍数,以下解决方案有效:

list = [['a'], ['b'], ['c'], ['1234'], ['d'], ['e'], ['g'], ['h'], ['i'], ['56']]
row = int(len(list)/5)
new_list = [i.tolist() for i in np.array(list).reshape((row,5))]
print (new_list)
导致

[['a', 'b', 'c', '1234', 'd'], ['e', 'g', 'h', 'i', '56']]

以下是一个非库列表理解解决方案:

>>> L = [['a'], ['b'], ['c'], ['1234'], ['d'], ['e'], ['g'], ['h'], ['i'], ['56']]
>>> [[j[0] for j in L[i:i+5]] for i in range(0, len(L), 5)]
[['a', 'b', 'c', '1234', 'd'], ['e', 'g', 'h', 'i', '56']]

顺便说一下,调用list
list
会重新定义一个内置函数。

会像大小均匀的块一样将+组展平吗?您的列表只包含一个元素?或者可以包含其他内容?map函数如何将“list”关键字作为第一个参数?map对第二个函数的所有元素调用第一个函数并返回一个生成器<代码>列表
不是关键字,它是一个函数。@karthikeayan
列表
不是关键字。它只是一个内置函数的名称。在这方面,它是一个正常变量,您可以为它分配任何您想要的:
list=“您不应该这样做”
。我正在做,而你已经发布了。但是,只有另一条路才能到达同一个目的地;)
[['a', 'b', 'c', '1234', 'd'], ['e', 'g', 'h', 'i', '56']]
>>> L = [['a'], ['b'], ['c'], ['1234'], ['d'], ['e'], ['g'], ['h'], ['i'], ['56']]
>>> [[j[0] for j in L[i:i+5]] for i in range(0, len(L), 5)]
[['a', 'b', 'c', '1234', 'd'], ['e', 'g', 'h', 'i', '56']]