【发布时间】:2022-12-16 23:21:03
【问题描述】:
我正在尝试更改以下代码以获得以下回报:
“1 2 3 ... 31 32 33 34 35 36 37 ... 63 64 65”
def createFooter2(current_page, total_pages, boundaries, around) -> str:
footer = []
page = 1
#Append lower boundaries
while page <= boundaries:
footer.append(page)
page += 1
#Append current page and arround
page = current_page - around
while page <= current_page + around:
footer.append(page)
page += 1
#Append upper boundaries
page = total_pages - boundaries + 1
while page <= total_pages:
footer.append(page)
page += 1
#Add Ellipsis if necessary
for i in range(len(footer)):
if i > 0 and footer[i] - footer[i - 1] > 1:
footer.insert(i, "...")
result = ' '.join(str(page) for page in result)
print(result)
return result
createFooter2(34, 65, 3, 3)
如果下一页不紧挨着它,我想在页面之间插入一个“...”。但是我无法插入列表。
我应该如何更改代码以使其工作?
【问题讨论】:
-
在我的脑海中,我首先列出了我必须添加'...'的索引,然后从较高的索引到较低的索引进行插入。