Python 切分数组,将一个数组均匀切分成多个数组
Python 切分数组
将一个数组,均分为多个数组
代码
# -*- coding:utf-8 -*-
# py3
def list_split(items, n):
return [items[i:i+n] for i in range(0, len(items), n)]
if '__main__' == __name__:
list1 = ['s1', 's2', 's3', 's4', 's5', 's6', 's7']
# 将数组 list1 每 3 个切分一次
list2 = list_split(list1, 3)
print(list2)
输出
[['s1', 's2', 's3'], ['s4', 's5', 's6'], ['s7']]