利用python实现一个底层分页的逻辑

from math import ceil


def pagination(data: list, p_per_count, p_current_page):
    # 使用ceil函数向上取整
    pagination_count = ceil(len(data) / per_count)
    while p_current_page - 1 < pagination_count:
        per_data = data[(p_current_page - 1) * p_per_count:p_current_page * p_per_count]
        print(f"第{p_current_page}页:{per_data}")
        p_current_page += 1


if __name__ == '__main__':
    # 待分页的数据
    city = ["上海", "北京", "南京", "无锡", "徐州", "常州", "苏州", "南通", "连云港", "淮安", "泰州"]
    # 每页显示多少条,接口中一般前端传递
    per_count = 2
    # 当前页,默认从1开始
    current_page = 1
    pagination(city, p_per_count=per_count, p_current_page=current_page)

你可能感兴趣的:(python测试开发,python,分类,算法)