python 对列表分块

本文探讨了如何将Python列表均匀分块,包括确保每块长度一致甚至使用默认值填充的方法。同时,文章提及了这些技巧在并行编程场景中的应用。对于想提升Python编程技能的程序员和程序师来说,这是一个实用的话题。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

在这里插入图片描述
每块长度几乎相同

def partition(lst, n):
    division = len(lst) / float(n)
    return [list(lst)[int(round(division * i)): int(round(division * (i + 1)))] for i in range(n)]

'''
>>> a = [i for i in range(100)]

>>> partition(a,10)

[[0, 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],
 [30, 31, 32, 33, 34, 35, 36, 37, 38, 39],
 [40, 41, 42, 43, 44, 45, 46, 47, 48, 49],
 [50, 51, 52, 53, 54, 55, 56, 57, 58, 59],
 [60, 61, 62, 63, 64, 65, 66, 67, 68, 69],
 [70, 71, 72, 73, 74, 75, 76, 77, 78, 79],
 [80, 81, 82, 83, 84, 85, 86, 87, 88, 89],
 [90, 91, 92, 93, 94, 95, 96, 97, 98, 99]]

>>> partition(a,11)
 
[[0, 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, 30, 31, 32, 33, 34, 35],
 [36, 37, 38, 39, 40, 41, 42, 43, 44],
 [45, 46, 47, 48, 49, 50, 51, 52, 53, 54],
 [55, 56, 57, 58, 59, 60, 61, 62, 63],
 [64, 65, 66, 67, 68, 69, 70, 71, 72],
 [73, 74, 75, 76, 77, 78, 79, 80, 81],
 [82, 83, 84, 85, 86, 87, 88, 89, 90],
 [91, 92, 93, 94, 95, 96, 97, 98, 99]]

>>> partition(a,7)

[[0, 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, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42],
 [43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56],
 [57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70],
 [71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85],
 [86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99]]

'''

每块长度相同,但需要用默认值填充

from six.moves import zip_longest

def grouper(n, iterable, padvalue=None):
    "grouper(3, 'abcdefg', 'x') --> ('a','b','c'), ('d','e','f'), ('g','x','x')"
    return zip_longest(*[iter(iterable)] * n, fillvalue=padvalue)

'''
>>> print(list(grouper(9,[i for i in range(100)])))

[(0, 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, 30, 31, 32, 33, 34, 35), 
(36, 37, 38, 39, 40, 41, 42, 43, 44), 
(45, 46, 47, 48, 49, 50, 51, 52, 53),
(54, 55, 56, 57, 58, 59, 60, 61, 62),
(63, 64, 65, 66, 67, 68, 69, 70, 71),
(72, 73, 74, 75, 76, 77, 78, 79, 80), 
(81, 82, 83, 84, 85, 86, 87, 88, 89), 
(90, 91, 92, 93, 94, 95, 96, 97, 98), 
(99, None, None, None, None, None, None, None, None)]
'''

并行编程中的案例:

    with open(file_) as f:
        with ProcessPoolExecutor(max_workers=cpu_count()) as executor:
            total = 0
            for adj_chunk in executor.map(parse_func, 
            							  grouper(int(chunksize), f)):
                adjlist.extend(adj_chunk)
                total += len(adj_chunk)

最后多说一句,小编是一名python开发工程师,这里有我自己整理了一套最新的python系统学习教程,包括从基础的python脚本到web开发、爬虫、数据分析、数据可视化、机器学习等。想要这些资料的可以关注小编,并在后台私信小编:“01”即可领取。
在这里插入图片描述

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值