在开发中遇到,需要将下发的任务平均分给n个工人,如何进行分配,写个函数进行记录。
1 | |
2 | def list_spilt(arr, n): | 
3 |     ''' | 
4 |     将列表平均分成n份 | 
5 |     :param list:  列表对象 | 
6 |     :param n:  份数 | 
7 |     :return: | 
8 |     ''' | 
9 |     if not isinstance(arr, list): | 
10 |         return False | 
11 |     if not isinstance(n, int) or n<=0 or not str(n).isdigit(): | 
12 |         return False | 
13 |     length = len(arr) | 
14 |     new_list = [] | 
15 |     for i in range(n): | 
16 |         one_list = arr[math.floor(i / n * length):math.floor((i + 1) / n * length)] | 
17 |         new_list.append(one_list) | 
18 |     return new_list |