用range()以反向顺序打印列表?

时间:2021-09-11 18:26:39

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

如何在Python中使用range()生成以下列表?

[9, 8, 7, 6, 5, 4, 3, 2, 1, 0]

16 个解决方案

#1


387  

use reversed() function:

使用反转()函数:

reversed(range(10))

It's much more meaningful.

这是更有意义。

Update:

更新:

If you want it to be a list (as btk pointed out):

如果您希望它是一个列表(正如btk所指出的):

list(reversed(range(10)))

Update:

更新:

If you want to use only range to achieve the same result, you can use all its parameters. range(start, stop, step)

如果您只想使用范围来达到相同的结果,您可以使用它的所有参数。范围(启动、停止、步骤)

For example, to generate a list [5,4,3,2,1,0], you can use the following:

例如,要生成一个列表[5、4、3、2、1、0],您可以使用以下内容:

range(5, -1, -1)

It may be less intuitive but as the comments mention, this is more efficient and the right usage of range for reversed list.

它可能不那么直观,但正如评论所提到的,这是更有效和正确的使用范围的反向列表。

#2


282  

Use the 'range' built-in function. The signature is range(start, stop, step). This produces a sequence that yields numbers, starting with start, and ending if stop has been reached, excluding stop.

使用“range”内置函数。签名是范围(开始、停止、步骤)。这将产生一个序列,该序列产生数字,从开始到结束,如果到达了stop,则不包含stop。

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

In Python 3, this produces a non-list range object, which functions effectively like a read-only list (but uses way less memory, particularly for large ranges).

在Python 3中,这将生成一个非列表范围对象,它的功能与只读列表类似(但使用的内存要少得多,尤其是在大范围内)。

#3


34  

You could userange(10)[::-1]which is the same thing asrange(9, -1, -1)and arguably more readable (if you're familiar with the commonsequence[::-1]Python idiom).

您可以使用userange(10)[::-1],它与range(9、-1、-1)是相同的,并且可能更易于阅读(如果您熟悉commonsequence[::-1]Python习语)。

#4


8  

for i in range(8, 0, -1)

will solve this problem. It will output 8 to 1, and -1 means a reversed list

将解决这个问题。它将输出8到1,-1表示反向列表

#5


6  

Readibility aside, reversed(range(n)) seems to be faster than range(n)[::-1].

除易读性外,反转(range(n))似乎比range(n)快[::-1]。

$ python -m timeit "reversed(range(1000000000))"
1000000 loops, best of 3: 0.598 usec per loop
$ python -m timeit "range(1000000000)[::-1]"
1000000 loops, best of 3: 0.945 usec per loop

Just if anyone was wondering :)

如果有人想知道:

#6


6  

For those who are interested in the "efficiency" of the options collected so far...

对于那些对到目前为止收集的选项的“效率”感兴趣的人……

Jaime RGP's answer led me to restart my computer after timing the somewhat "challenging" solution of Jason literally following my own suggestion (via comment). To spare the curious of you the downtime, I present here my results (worst-first):

杰米·RGP的回答让我重新启动了我的电脑,在此之前,我按照我自己的建议(通过评论)安排了有点“挑战性”的Jason解决方案。为了不让好奇的你感到停机时间,我在这里展示我的结果(最坏的):

Jason's answer (maybe just an excursion into the power of list comprehension):

杰森的回答(也许只是一个对列表理解能力的考察):

$ python -m timeit "[9-i for i in range(10)]"
1000000 loops, best of 3: 1.54 usec per loop

martineau's answer (readable if you are familiar with the extended slices syntax):

martineau的答案(如果你熟悉扩展片的语法):

$ python -m timeit "range(10)[::-1]"
1000000 loops, best of 3: 0.743 usec per loop

Michał Šrajer's answer (the accepted one, very readable):

MichałŠrajer的回答(接受,非常可读):

$ python -m timeit "reversed(range(10))"
1000000 loops, best of 3: 0.538 usec per loop

bene's answer (the very first, but very sketchy at that time):

bene的回答(第一次,但当时很粗略):

$ python -m timeit "range(9,-1,-1)"
1000000 loops, best of 3: 0.401 usec per loop

The last option is easy to remember using the range(n-1,-1,-1) notation by Val Neekman.

最后一个选项很容易记住使用Val Neekman使用的range(n-1,-1,-1)符号。

#7


5  

The requirement in this question calls for a list of integers of size 10 in descending order. So, let's produce a list in python.

此问题中的需求要求按降序排列大小为10的整数。让我们用python生成一个列表。

# This meets the requirement.
# But it is a bit harder to wrap one's head around this. right?
>>> range(10-1, -1, -1)
[9, 8, 7, 6, 5, 4, 3, 2, 1, 0]

# let's find something that is a bit more self-explanatory. Sounds good?
# ----------------------------------------------------

# This returns a list in ascending order.
# Opposite of what the requirement called for.
>>> range(10)
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

# This returns an iterator in descending order.
# Doesn't meet the requirement as it is not a list.
>>> reversed(range(10))
<listreverseiterator object at 0x10e14e090>

# This returns a list in descending order and meets the requirement
>>> list(reversed(range(10)))
[9, 8, 7, 6, 5, 4, 3, 2, 1, 0]

#8


5  

No sense to use reverse as range method can return reversed list.

没有意义使用反向作为范围方法可以返回反向列表。

When you have iteration over n items and want to replace order of list returned by range(start, stop, step) you have to use third parameter of range which identifies step and set it to -1, other parameters shall be adjusted accordingly:

当你对n个项目进行迭代,想要替换range(start, stop, step)返回的list的顺序时,你必须使用range的第三个参数来标识步骤并将其设置为-1,其他参数也需要相应调整:

  1. Provide stop parameter as -1(it's previous value of stop - 1, stop was equal to 0).
  2. 提供stop参数为-1(它之前的值为stop -1, stop = 0)。
  3. As start parameter use n-1.
  4. 作为启动参数,使用n-1。

So equivalent of range(n) in reverse order would be:

所以反过来的(n)范围的等价是:

n = 10
print range(n-1,-1,-1) 
#[9, 8, 7, 6, 5, 4, 3, 2, 1, 0]

#9


2  

i believe this can help,

我相信这能有所帮助,

range(5)[::-1]

below is Usage:

下面是使用方法:

for i in range(5)[::-1]:
    print i 

#10


1  

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

#11


1  

You can do printing of reverse numbers with range() BIF Like ,

你可以用range()来打印反数,

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

Output will be [10,9,8,7,6,5,4,3,2,1]

输出将(10、9、8、7、6、5、4、3、2、1]

range() - range ( start , end , increment/decrement ) where start is inclusive , end is exclusive and increment can be any numbers and behaves like step

range() - range(start, end, increment/ dec浸润)在包含start的地方,end是排他性的,increment可以是任意的数字,并且像step一样

#12


0  

Using without [::-1] or reversed -

不使用[::-1]或反向使用

def reverse(text):
    result = []
    for index in range(len(text)-1,-1,-1):
        c = text[index]
        result.append(c)
    return ''.join(result)

print reverse("python!")

#13


0  

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

#14


0  

You don't necessarily need to use the range function, you can simply do list[::-1] which should return the list in reversed order swiftly, without using any additions.

您不需要使用range函数,您只需执行list[::-1],该列表应该以反向顺序快速返回列表,而不需要添加任何内容。

#15


0  

Suppose you have a list call it a={1,2,3,4,5} Now if you want to print the list in reverse then simply use the following code.

假设有一个列表,调用它a={1,2,3,4,5}如果想要反向打印列表,那么只需使用以下代码。

a.reverse
for i in a:
   print(i)

I know you asked using range but its already answered.

我知道你用range函数问的,但是已经有答案了。

#16


-1  

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.

你不会得到0的。例如,假设你的10不是一个神奇的数字,而是一个你用来从反向查找的变量。如果n为0,反转(range(0))将不会执行,如果您碰巧在0索引中有一个对象,那么这是错误的。

#1


387  

use reversed() function:

使用反转()函数:

reversed(range(10))

It's much more meaningful.

这是更有意义。

Update:

更新:

If you want it to be a list (as btk pointed out):

如果您希望它是一个列表(正如btk所指出的):

list(reversed(range(10)))

Update:

更新:

If you want to use only range to achieve the same result, you can use all its parameters. range(start, stop, step)

如果您只想使用范围来达到相同的结果,您可以使用它的所有参数。范围(启动、停止、步骤)

For example, to generate a list [5,4,3,2,1,0], you can use the following:

例如,要生成一个列表[5、4、3、2、1、0],您可以使用以下内容:

range(5, -1, -1)

It may be less intuitive but as the comments mention, this is more efficient and the right usage of range for reversed list.

它可能不那么直观,但正如评论所提到的,这是更有效和正确的使用范围的反向列表。

#2


282  

Use the 'range' built-in function. The signature is range(start, stop, step). This produces a sequence that yields numbers, starting with start, and ending if stop has been reached, excluding stop.

使用“range”内置函数。签名是范围(开始、停止、步骤)。这将产生一个序列,该序列产生数字,从开始到结束,如果到达了stop,则不包含stop。

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

In Python 3, this produces a non-list range object, which functions effectively like a read-only list (but uses way less memory, particularly for large ranges).

在Python 3中,这将生成一个非列表范围对象,它的功能与只读列表类似(但使用的内存要少得多,尤其是在大范围内)。

#3


34  

You could userange(10)[::-1]which is the same thing asrange(9, -1, -1)and arguably more readable (if you're familiar with the commonsequence[::-1]Python idiom).

您可以使用userange(10)[::-1],它与range(9、-1、-1)是相同的,并且可能更易于阅读(如果您熟悉commonsequence[::-1]Python习语)。

#4


8  

for i in range(8, 0, -1)

will solve this problem. It will output 8 to 1, and -1 means a reversed list

将解决这个问题。它将输出8到1,-1表示反向列表

#5


6  

Readibility aside, reversed(range(n)) seems to be faster than range(n)[::-1].

除易读性外,反转(range(n))似乎比range(n)快[::-1]。

$ python -m timeit "reversed(range(1000000000))"
1000000 loops, best of 3: 0.598 usec per loop
$ python -m timeit "range(1000000000)[::-1]"
1000000 loops, best of 3: 0.945 usec per loop

Just if anyone was wondering :)

如果有人想知道:

#6


6  

For those who are interested in the "efficiency" of the options collected so far...

对于那些对到目前为止收集的选项的“效率”感兴趣的人……

Jaime RGP's answer led me to restart my computer after timing the somewhat "challenging" solution of Jason literally following my own suggestion (via comment). To spare the curious of you the downtime, I present here my results (worst-first):

杰米·RGP的回答让我重新启动了我的电脑,在此之前,我按照我自己的建议(通过评论)安排了有点“挑战性”的Jason解决方案。为了不让好奇的你感到停机时间,我在这里展示我的结果(最坏的):

Jason's answer (maybe just an excursion into the power of list comprehension):

杰森的回答(也许只是一个对列表理解能力的考察):

$ python -m timeit "[9-i for i in range(10)]"
1000000 loops, best of 3: 1.54 usec per loop

martineau's answer (readable if you are familiar with the extended slices syntax):

martineau的答案(如果你熟悉扩展片的语法):

$ python -m timeit "range(10)[::-1]"
1000000 loops, best of 3: 0.743 usec per loop

Michał Šrajer's answer (the accepted one, very readable):

MichałŠrajer的回答(接受,非常可读):

$ python -m timeit "reversed(range(10))"
1000000 loops, best of 3: 0.538 usec per loop

bene's answer (the very first, but very sketchy at that time):

bene的回答(第一次,但当时很粗略):

$ python -m timeit "range(9,-1,-1)"
1000000 loops, best of 3: 0.401 usec per loop

The last option is easy to remember using the range(n-1,-1,-1) notation by Val Neekman.

最后一个选项很容易记住使用Val Neekman使用的range(n-1,-1,-1)符号。

#7


5  

The requirement in this question calls for a list of integers of size 10 in descending order. So, let's produce a list in python.

此问题中的需求要求按降序排列大小为10的整数。让我们用python生成一个列表。

# This meets the requirement.
# But it is a bit harder to wrap one's head around this. right?
>>> range(10-1, -1, -1)
[9, 8, 7, 6, 5, 4, 3, 2, 1, 0]

# let's find something that is a bit more self-explanatory. Sounds good?
# ----------------------------------------------------

# This returns a list in ascending order.
# Opposite of what the requirement called for.
>>> range(10)
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

# This returns an iterator in descending order.
# Doesn't meet the requirement as it is not a list.
>>> reversed(range(10))
<listreverseiterator object at 0x10e14e090>

# This returns a list in descending order and meets the requirement
>>> list(reversed(range(10)))
[9, 8, 7, 6, 5, 4, 3, 2, 1, 0]

#8


5  

No sense to use reverse as range method can return reversed list.

没有意义使用反向作为范围方法可以返回反向列表。

When you have iteration over n items and want to replace order of list returned by range(start, stop, step) you have to use third parameter of range which identifies step and set it to -1, other parameters shall be adjusted accordingly:

当你对n个项目进行迭代,想要替换range(start, stop, step)返回的list的顺序时,你必须使用range的第三个参数来标识步骤并将其设置为-1,其他参数也需要相应调整:

  1. Provide stop parameter as -1(it's previous value of stop - 1, stop was equal to 0).
  2. 提供stop参数为-1(它之前的值为stop -1, stop = 0)。
  3. As start parameter use n-1.
  4. 作为启动参数,使用n-1。

So equivalent of range(n) in reverse order would be:

所以反过来的(n)范围的等价是:

n = 10
print range(n-1,-1,-1) 
#[9, 8, 7, 6, 5, 4, 3, 2, 1, 0]

#9


2  

i believe this can help,

我相信这能有所帮助,

range(5)[::-1]

below is Usage:

下面是使用方法:

for i in range(5)[::-1]:
    print i 

#10


1  

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

#11


1  

You can do printing of reverse numbers with range() BIF Like ,

你可以用range()来打印反数,

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

Output will be [10,9,8,7,6,5,4,3,2,1]

输出将(10、9、8、7、6、5、4、3、2、1]

range() - range ( start , end , increment/decrement ) where start is inclusive , end is exclusive and increment can be any numbers and behaves like step

range() - range(start, end, increment/ dec浸润)在包含start的地方,end是排他性的,increment可以是任意的数字,并且像step一样

#12


0  

Using without [::-1] or reversed -

不使用[::-1]或反向使用

def reverse(text):
    result = []
    for index in range(len(text)-1,-1,-1):
        c = text[index]
        result.append(c)
    return ''.join(result)

print reverse("python!")

#13


0  

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

#14


0  

You don't necessarily need to use the range function, you can simply do list[::-1] which should return the list in reversed order swiftly, without using any additions.

您不需要使用range函数,您只需执行list[::-1],该列表应该以反向顺序快速返回列表,而不需要添加任何内容。

#15


0  

Suppose you have a list call it a={1,2,3,4,5} Now if you want to print the list in reverse then simply use the following code.

假设有一个列表,调用它a={1,2,3,4,5}如果想要反向打印列表,那么只需使用以下代码。

a.reverse
for i in a:
   print(i)

I know you asked using range but its already answered.

我知道你用range函数问的,但是已经有答案了。

#16


-1  

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.

你不会得到0的。例如,假设你的10不是一个神奇的数字,而是一个你用来从反向查找的变量。如果n为0,反转(range(0))将不会执行,如果您碰巧在0索引中有一个对象,那么这是错误的。