def paginate_data(data_list, size_page, current_page):
"""
数据分页函数
:param data_list: list, 数据列表
:param size_page: int, 每页的数量
:param current_page: int, 当前页码
:return: tuple, (总页数, 当前页码, 当前页的数据列表)
"""
if size_page <= 0:
raise ValueError("每页数量必须大于0")
if current_page <= 0:
raise ValueError("当前页码必须大于0")
# 计算总页数
total_page = (len(data_list) + size_page - 1) // size_page
# 校正当前页码
current_page = min(current_page, total_page)
# 计算当前页数据的起始索引和结束索引
start_index = (current_page - 1) * size_page
end_index = start_index + size_page
# 获取当前页的数据列表
result_data_list = data_list[start_index:end_index]
return total_page, current_page, result_data_list
# 示例用法
data_list = [i for i in range(100)] # 0到99的数
size_page = 10
current_page = 3
total_page, current_page, result_data_list = paginate_data(data_list, size_page, current_page)
print(f"总页数: {total_page}")
print(f"当前页: {current_page}")
print(f"当前页数据列表: {result_data_list}")
标签:定义数据,python,list,param,分页,data,page,size
From: https://www.cnblogs.com/lanjianhua/p/18556769