Print a list in reverse order with range()?

前端 未结 19 752
长发绾君心
长发绾君心 2020-11-30 17:09

How can you produce the following list with range() in Python?

[9, 8, 7, 6, 5, 4, 3, 2, 1, 0]
19条回答
  •  借酒劲吻你
    2020-11-30 17:40

    range(9,-1,-1)
        [9, 8, 7, 6, 5, 4, 3, 2, 1, 0]
    

    Is the correct form. If you use

    reversed(range(10))
    

    you wont get a 0 case. For instance, say your 10 isn't a magic number and a variable you're using to lookup start from reverse. If your n case is 0, reversed(range(0)) will not execute which is wrong if you by chance have a single object in the zero index.

提交回复
热议问题