1. 函數(shù)
import math
def split_list(data, num_page):
'''
num_page: 打算分成多少個小列表 int類型
data: 為一個待分割的列表 list or tuple or set 類型
'''
data_list = list()
try:
n = len(data) // num_page
except Exception as e:
n = len(data)
if n == 0:
n = len(data)
for i in range(0, len(data), n):
a = data[i: i + n]
data_list.append(a)
return data_list
2. 測試
if __name__ == '__main__':
a = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17]
result = split_list(a, 3)
print(result)
結(jié)果:
[[1, 2, 3, 4, 5, 6], [7, 8, 9, 10, 11, 12], [13, 14, 15, 16, 17]]