Print a list in reverse order with range()?

前端 未结 19 747
长发绾君心
长发绾君心 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条回答
  •  Happy的楠姐
    2020-11-30 17:33

    because range(n) produces an iterable there are all sorts of nice things you can do which will produce the result you desire, such as:

    range(n)[::-1]
    

    if loops are ok, we can make sort of a queue:

    a = []
    for i in range(n):
        a.insert(0,a)
    return a
    

    or maybe use the reverse() method on it:

    reverse(range(n))
    

提交回复
热议问题