减少 Python 中的循环是不可能的?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4294082/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-18 15:07:11  来源:igfitidea点击:

Decreasing for loops in Python impossible?

pythonloopsfor-loop

提问by Gal

I could be wrong (just let me know and I'll delete the question) but it seems python won't respond to

我可能是错的(请告诉我,我会删除问题)但似乎 python 不会响应

for n in range(6,0):
    print n

I tried using xrange and it didn't work either. How can I implement that?

我尝试使用 xrange 但它也不起作用。我该如何实施?

采纳答案by Steve Tjoa

for n in range(6,0,-1):
    print n
# prints [6, 5, 4, 3, 2, 1]

回答by cji

for n in range(6,0,-1):
    print n

回答by vanza

>>> range(6, 0, -1)
[6, 5, 4, 3, 2, 1]

回答by pratikm

This is very late, but I just wanted to add that there is a more elegant way: using reversed

这已经很晚了,但我只想补充一点,有一种更优雅的方式:使用 reversed

for i in reversed(range(10)):
    print i

gives:

给出:

4
3
2
1
0

回答by Handy Jodana

for n in range(6,0,-1)

This would give you 6,5,4,3,2,1

这会给你 6,5,4,3,2,1

As for

至于

for n in reversed(range(0,6))

would give you 5,4,3,2,1,0

会给你 5,4,3,2,1,0

回答by Neo

0 is conditional value when this condition is true, loop will keep executing.10 is the initial value. 1 is the modifier where may be simple decrement.

0 为条件值,当此条件为真时,循环将继续执行。10 为初始值。1 是修饰符,可能是简单的递减。

for number in reversed(range(0,10,1)):
print number;

回答by Mark Moretto

Late to the party, but for anyone tasked with creating their own or wants to see how this would work, here's the function with an added bonus of rearranging the start-stop values based on the desired increment:

晚会晚了,但对于任何负责创建自己的任务或想看看这将如何工作的人来说,这里有一个额外的好处,即根据所需的增量重新排列开始-停止值:

def RANGE(start, stop=None, increment=1):
    if stop is None:
        stop = start
        start = 1

    value_list = sorted([start, stop])

    if increment == 0:
        print('Error! Please enter nonzero increment value!')
    else:
        value_list = sorted([start, stop])
        if increment < 0:
            start = value_list[1]
            stop = value_list[0]
            while start >= stop:
                worker = start
                start += increment
                yield worker
        else:
            start = value_list[0]
            stop = value_list[1]
            while start < stop:
                worker = start
                start += increment
                yield worker

Negative increment:

负增量:

for i in RANGE(1, 10, -1):
    print(i)

Or, with start-stop reversed:

或者,起止颠倒:

for i in RANGE(10, 1, -1):
    print(i)

Output:

输出:

10
9
8
7
6
5
4
3
2
1

Regular increment:

常规增量:

for i in RANGE(1, 10):
    print(i)

Output:

输出:

1
2
3
4
5
6
7
8
9

Zero increment:

零增量:

for i in RANGE(1, 10, 0):
    print(i)

Output:

输出:

'Error! Please enter nonzero increment value!'

回答by SREERAG R NANDAN

For python3 where -1indicate the value that to be decremented in each step for n in range(6,0,-1): print(n)

对于python3,其中-1指示在每一步中要递减的值 for n in range(6,0,-1): print(n)