每块长度几乎相同
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”即可领取。