python for 循环中的range怎么用_在for循环中使用range()

I''m reading Text Processing in Python right now and I came across a

comment that is helping me to see for loops in a new light. I think

because I''m used to the C-style for loop where you create a counter

within the loop declaration, for loops have always seemed to me to be

about doing something a certain number of times, and not about iterating

over an object.

The reason for this distinction comes from the fact that I read a lot

how using range and for is somewhat discouraged, because it doesn''t

really use a for loop for it''s true purpose. So my question is, is this

just a Python-oriented opinion about for loops, or is it a general idea?

Also, what if you *do* need to just do something a set number of times.

Is this okay, or does it mean you are approaching the problem

incorrectly? Using for and range together seems to be a common idiom,

yet at the same time discouraged, so I''m wondering what is a good balance.

Thanks.

解决方案John Salerno writes:The reason for this distinction comes from the fact that I read a lot

how using range and for is somewhat discouraged, because it doesn''t

really use a for loop for it''s true purpose. So my question is, is

this just a Python-oriented opinion about for loops, or is it a

general idea?

Normally you''d use range or xrange. range builds a complete list in

memory so can be expensive if the number is large. xrange just counts

up to that number.

hi John,

Python doesn''t provide for loop like C / C++ but using Range() or

Xrange() you can achive all the functionalities of the C for loop.If

you wants distributed for loop You can use Xrange.

John Salerno wrote:I''m reading Text Processing in Python right now and I came across a

comment that is helping me to see for loops in a new light. I think

because I''m used to the C-style for loop where you create a counter

within the loop declaration, for loops have always seemed to me to be

about doing something a certain number of times, and not about iterating

over an object.

The reason for this distinction comes from the fact that I read a lot

how using range and for is somewhat discouraged, because it doesn''t

really use a for loop for it''s true purpose. So my question is, is this

just a Python-oriented opinion about for loops, or is it a general idea?

Also, what if you *do* need to just do something a set number of times.

Is this okay, or does it mean you are approaching the problem

incorrectly? Using for and range together seems to be a common idiom,

yet at the same time discouraged, so I''m wondering what is a good balance.

Thanks.

John Salerno wrote:The reason for this distinction comes from the fact that I read a lot

how using range and for is somewhat discouraged, because it doesn''t

really use a for loop for it''s true purpose. So my question is, is this

just a Python-oriented opinion about for loops, or is it a general idea?

The use of range in for loops is not discouraged, just the unnecessary

use of it. In many languages, you were forced to use range - or size,

or length, or count, etc - because the only practical method of

iteration was to use successive indices into the array or other

collection, stopping when the indexes run out. Most loops operate on

some sort of structure in this way.

In Python, you can iterate directly over the structure instead, so you

no longer have to worry about keeping track of the current index,

comparing it to the maximum index, or collecting the right object from

the structure according to its index. Python just gives you the next

object until there are none left. As a result, for most looping tasks,

you have no need to use range or an equivalent.

Also, what if you *do* need to just do something a set number of times.

Is this okay, or does it mean you are approaching the problem

incorrectly?

Do you really need to do something "a number of times"? If so, range

(or xrange) is perfect for the job. More often though, you need to do

something "once for every ", so put your ''whatevers'' in a

list and iterate over that.

--

Ben Sizer

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值