def split_list(lst, num=2):
'''
等分列表
@params lst --> 要分的列表;
@params num --> 要等分的数量;
'''
length = len(lst)
split_length = length // num
remainder = length % num
result = []
index = 0
for _ in range(num):
sublist_length = split_length + (1 if remainder > 0 else 0)
sublist = lst[index:index+sublist_length]
result.append(sublist)
index += sublist_length
remainder -= 1
return result
a = (1, 2,3,4,5,6,7,8,9, 10)
print(split_list(a, num=3))
结果: [(1, 2, 3, 4), (5, 6, 7), (8, 9, 10)]
“相关推荐”对你有帮助么?
-
非常没帮助
-
没帮助
-
一般
-
有帮助
-
非常有帮助
提交