将列表划分成块的更快方法

时间:2018-07-16 16:34:23

标签: python-3.x iterator

我有一个dict列表,需要将其分为多个块:

input_size = len(input_rows)  # num of dicts
slice_size = int(input_size / 4)  # size of each chunk
remain = input_size % 4  # num of remaining dicts which cannot be divided into chunks
result = []  # initializes the list for containing lists of dicts
iterator = iter(input_rows)  # gets a iterator on input
for i in range(4):
    result.append([])  # creates an empty list as an element/block in result for containing rows for each core
    for j in range(slice_size):
        result[i].append(iterator.__next__())  # push in rows into the current list
    if remain:
        result[i].append(iterator.__next__())  # push in one remainder row into the current list
        remain -= 1

input_rows包含dict的列表,将其分为4个块/切片;如果有剩余的dict不能平均分为4个块,则这些剩余的dict将放入一些块中。列表(result)用于包含每个块,而每个块又包含dict的列表。

我想知道如何以更有效的方式做到这一点。

3 个答案:

答案 0 :(得分:2)

使用标准库

R = list()
L = list(range(10))

remainder = int(len(L) % 4)
chunk_size = int(len(L) / 4)
position = 0

while position < len(L):
    this_chunk = chunk_size
    if remainder:
        this_chunk += 1
        remainder -= 1
    R.append(L[position:this_chunk + position])
    position += this_chunk

print(R)
[[0, 1, 2], [3, 4, 5], [6, 7], [8, 9]]

这应该更快,因为您要迭代和插入的内容要少得多。在这种情况下,您实际上只是根据列表元数据的计算来抓取4个切片并插入4次...

此外,这是numpy.array_split *的具体原因:这应该还是更快...

>>> print(*np.array_split(range(10), 4))
[0 1 2] [3 4 5] [6 7] [8 9]

编辑:由于评论部分的反馈和以上答案中的潜在错误(在输入列表大小小于潜在的块数的情况下),这里是一个替代函数,其功能相同,但会总是产生请求数量的块

def array_split(input_list, chunks):
    chunk_size = int(len(input_list) / chunks)
    remainder = len(input_list) % chunks
    new_list = list()
    position = 0

    while position < len(input_list):
        this_chunk = chunk_size
        if remainder:
            this_chunk, remainder = this_chunk + 1, remainder - 1
        new_list.append(input_list[position:this_chunk + position])
        position += this_chunk

    new_list.extend([[] for _ in range(chunks - len(new_list))])

    return new_list

def unit_test():
    L = [1, 2]
    print(array_split(L, 4))

    L = list(range(13))
    print(array_split(L, 3))

    L = list(range(22))
    print(array_split(L, 5))

>>> unit_test()
[[1], [2], [], []]
[[0, 1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12]]
[[0, 1, 2, 3, 4], [5, 6, 7, 8, 9], [10, 11, 12, 13], [14, 15, 16, 17], [18, 19, 20, 21]]

答案 1 :(得分:1)

myList = [1,2,3,4,5,6,9]
numOfChunks = 2
newList = []

for i in range(0, len(myList), numOfChunks):
  newList.append(myList[i:i + numOfChunks])

print (newList) # [[1, 2], [3, 4], [5, 6], [9]]

答案 2 :(得分:1)

类似的东西不会很快被关闭,所以我有 使tixxit’s great answer from 2010适应了这个问题,将 我希望他的生成器能够理解列表,并使事情变得更容易 了解。

chunks = 4
quot, rem = divmod(len(input_rows), chunks)
divpt = lambda i: i * quot + min(i, rem)
return [input_rows[divpt(i):divpt(i+1)] for i in range(chunks)]

下面的测试框架显示了生成的代码 结果与OP的代码完全相同。

def main():
    for top in range(1, 18):
        print("{}:".format(top))
        input_list = list(range(1, top + 1))

        daiyue = chunkify_daiyue(input_list[:])
        print('daiyue: {}'.format(daiyue))

        zych = chunkify_zych(input_list[:])
        match = 'Same' if (zych == daiyue) else 'Different'
        print('Zych:   {}   {}'.format(zych, match))

        print('')


def chunkify_daiyue(input_rows):
    "Divide into chunks with daiyue's code"

    input_size = len(input_rows)  # num of dicts
    slice_size = int(input_size / 4)  # size of each chunk
    remain = input_size % 4  # num of remaining dicts which cannot be divided into chunks

    result = []  # initializes the list for containing lists of dicts
    iterator = iter(input_rows)  # gets a iterator on input

    for i in range(4):
        # creates an empty list as an element/block in result for
        # containing rows for each core
        result.append([])

        for j in range(slice_size):
            # push in rows into the current list
            result[i].append(iterator.__next__())
        if remain:
            # push in one remainder row into the current list
            result[i].append(iterator.__next__())
            remain -= 1

    return result


def chunkify_zych(input_rows):
    "Divide into chunks with Tom Zych's code"

    chunks = 4
    quot, rem = divmod(len(input_rows), chunks)
    divpt = lambda i: i * quot + min(i, rem)
    return [input_rows[divpt(i):divpt(i+1)] for i in range(chunks)]


if __name__ == '__main__':
    main()