分页程序,可以作为面试题

始终使当前页面,尽可能的在中间
#!/usr/bin/env python
#encoding=utf8
list=range(1,15)
print list

def show(number,list):
    try:
        idx=list.index(number)
        print idx
    except:
        return list[:5]
    a=idx-2
    b=idx+2
    start,end=None,None
    if a>=0:
        start=a
    else:
        b=abs(a)+b
        start=0
    if b<=(len(list)-1):
        end=b
    else:
        val=a-(b-(len(list)-1))
        if val<0:
            start=0
        else:
            start=val
        end=len(list)-1
    return list[start:end+1]

print show(19,list)

[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14]
[1, 2, 3, 4, 5]


原文地址:https://www.cnblogs.com/lexus/p/1791062.html