Python递减循环

Python decrementing loop

我有一个循环,但它是递增的。递减循环的语法是什么?

for item2 in textPad.get(itemindex,END):
    tagnames = textPad.tag_names(str(curline)+"."+str(curchar))
    if not "phpsingqoute" in tagnames and not "phpdoubqoute" in tagnames and not "phpcomment" in tagnames:
        if item2 == theopenandclose[1]:
            opencount -= 1
            if opencount == 0:
                textPad.tag_add(tagname,str(curline)+"."+str(curchar))
                textPad.tag_config(tagname,foreground="white",background="#000")
                break
        if item2 == theopenandclose[0]:
            opencount += 1
    if re.match(r'\n',item2):
        curline += 1
        curchar = -1
    curchar += 1

如果textPad.getreturns一个列表,可以使用reversed

for item2 in reversed(textPad.get(itemindex,END)):

这也适用于其他一些可迭代类型,但不是全部。 See the documentation. 如果得到 TypeError,请先尝试将 textPad.get 的结果转换为列表。:

for item2 in reversed(list(textPad.get(itemindex,END))):

无法添加评论 答案是 reversed(...)

Post before in this forum