如何在python中进行反向循环? [英] How to make a reverse for loop in python?

查看:589
本文介绍了如何在python中进行反向循环?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好


我是python的新手,我无法弄清楚如何在python中为

循环编写一个反向


例如python相当于c ++循环


for(i = 10; i> = 0; --i)

Hello

I''m new to python and i can''t figure out how to write a reverse for
loop in python

e.g. the python equivalent to the c++ loop

for (i = 10; i >= 0; --i)

推荐答案

Alex Snastaécrit:
Alex Snast a écrit :

你好


我是python的新手,我可以要弄清楚如何在python中写一个反向的
循环


例如python相当于c ++循环


for(i = 10; i> = 0; --i)
Hello

I''m new to python and i can''t figure out how to write a reverse for
loop in python

e.g. the python equivalent to the c++ loop

for (i = 10; i >= 0; --i)



为(i = 0; i <10; i--) - 对于范围(10)中的i:


for(i = 10; i> = 0; - -i)-for i in range(10,-1,-1):


Thoma

for (i = 0; i < 10; i--) -for i in range(10):

for (i = 10; i >= 0; --i) -for i in range(10,-1,-1):

Thoma


9月20日, 11:16 ??? am,Alex Snast< asn ... @ gmail.comwrote:
On Sep 20, 11:16???am, Alex Snast <asn...@gmail.comwrote:

你好


我是python的新手,我无法弄清楚如何在python中编写一个反向的
循环


例如python相当于c ++循环


for(i = 10; i> = 0; --i)
Hello

I''m new to python and i can''t figure out how to write a reverse for
loop in python

e.g. the python equivalent to the c++ loop

for (i = 10; i >= 0; --i)


>> for x in xrange(10,-1,-1):print i,
>>for i in xrange(10,-1,-1): print i,



10 9 8 7 6 5 4 3 2 1 0


注意起始编号为10,结尾为

数字为-1,因为你想要包含0

,步长是-1。

10 9 8 7 6 5 4 3 2 1 0

Note the starting number is 10, the ending
number is -1 because you want to include 0
and the step size is -1.


Alex Snast< ;作为**** @ gmail.comwrote:
Alex Snast <as****@gmail.comwrote:

你好


我是python的新手,我可以要弄清楚如何在python中写一个反向的
循环


例如python相当于c ++循环


for(i = 10; i> = 0; --i)
Hello

I''m new to python and i can''t figure out how to write a reverse for
loop in python

e.g. the python equivalent to the c++ loop

for (i = 10; i >= 0; --i)



确切的等价物是:


我的范围(10,-1,-1):打印我


除了你几乎永远不想在Python中这样做。不要指望

将语句从一种语言翻译成另一种语言:通常在

Python中,您将直接迭代您想要处理的序列

而不是试图计算所有电线杆错误的循环索引

结果。


迭代序列的常用方法反过来是:


for x in reversed(seq):print x


虽然你知道它是一个列表,字符串或其他对象支持

扩展切片你也可以这样做:


for x in seq [:: - 1]:print x


这可能不如使用''反向''那么明确,但是如果你只想做部分

一个明确的开始,停止和步骤>
序列。

The exact equivalent would be:

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

except you virtually never want to do that in Python. Don''t expect just to
translate statement by statement from one language to another: normally in
Python you will iterate directly over the sequence you want to process
rather than trying to count loop indices with all the telegraph pole errors
that result.

The usual way to iterate over a sequence in reverse is:

for x in reversed(seq): print x

although if you know it is a list, string or other object that supports
extended slicing you can also do:

for x in seq[::-1]: print x

this may be less clear than using ''reversed'', but does allow you to specify
an explicit start, stop and step if you want to do only part of the
sequence.


这篇关于如何在python中进行反向循环?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆