Python中的for-loops [英] For-loops in Python

查看:167
本文介绍了Python中的for-loops的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  for(v = n / 2  -  1; v> ; = 0; v--)

我其实已经尝试过Google,但据我所见唯一的解决方案是使用

解决方案

(x)():

$ (或者,在Python 3中)。

x, range()而不是 xrange()。) // 是地板部门,它确保结果是一个整数。


What is the best way of doing this in Python?

for (v = n / 2 - 1; v >= 0; v--)

I actually tried Google first, but as far as I can see the only solution would be to use while.

解决方案

The way to do it is with xrange():

for v in xrange(n // 2 - 1, -1, -1):

(Or, in Python 3.x, with range() instead of xrange().) // is flooring division, which makes sure the result is a whole number.

这篇关于Python中的for-loops的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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