在for循环中使用range()真的是Pythonic吗? [英] Is using range() in for loops really Pythonic?

查看:73
本文介绍了在for循环中使用range()真的是Pythonic吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道它很流行且非常方便,但我很好奇是否有纯粹主义者

那些认为使用类似的东西:


for x in range(10):

#do something 10次


是unPythonic。我问的原因是因为for循环

的结构似乎是为了迭代序列。看起来有点像是b $ b人工使用for循环来做一些特定数量的

次,如上所述。


任何人在那里拒绝以这种方式使用它,或者只是不可能避免

I know it''s popular and very handy, but I''m curious if there are purists
out there who think that using something like:

for x in range(10):
#do something 10 times

is unPythonic. The reason I ask is because the structure of the for loop
seems to be for iterating through a sequence. It seems somewhat
artificial to use the for loop to do something a certain number of
times, like above.

Anyone out there refuse to use it this way, or is it just impossible to
avoid?

推荐答案

John Salerno写道:
John Salerno wrote:

我知道它很受欢迎且非常方便,但我很好奇是否有纯粹主义者

那里谁认为使用类似的东西:


for x in range(10):

#do something 10次


是unPythonic。我问的原因是因为for循环

的结构似乎是为了迭代序列。看起来有点像是b $ b人工使用for循环来做一些特定数量的

次,如上所述。


任何人在那里拒绝以这种方式使用它,或者只是不可能避免

I know it''s popular and very handy, but I''m curious if there are purists
out there who think that using something like:

for x in range(10):
#do something 10 times

is unPythonic. The reason I ask is because the structure of the for loop
seems to be for iterating through a sequence. It seems somewhat
artificial to use the for loop to do something a certain number of
times, like above.

Anyone out there refuse to use it this way, or is it just impossible to
avoid?



嗯,你应该使用xrange(10)而不是范围(10)。虽然range()

返回每个数字的列表,但xrange()返回一个可迭代的
对象,只能按需生成它们,所以xrange(1)和

xrange(sys.maxint)将使用相同数量的RAM。


(当然,当它只有10个项目时,这并不重要。 )除此之外,b for x inrange(10)是一个标准的Python成语,即使它有点奇怪。

-

Well, you should use "xrange(10)" instead of "range(10)". While range()
returns a list of every single number, xrange() returns an iterable
object that only generates them on-demand, so xrange(1) and
xrange(sys.maxint) will use the same amount of RAM.

(Not that it matters when it''s only 10 items, of course.)

Other than that, "for i in xrange(10)" is a standard Python idiom, even
though it is a bit weird.
--


5月10日,8:19 * pm,John Salerno< johnj ... @ NOSPAMgmail.com写道:
On May 10, 8:19*pm, John Salerno <johnj...@NOSPAMgmail.comwrote:

我知道它很受欢迎,非常方便,但我我很好奇是否有纯粹主义者

那些人认为使用类似的东西:


for x in range(10):

* * #do的东西10次


是unPythonic。我问的原因是因为for循环

的结构似乎是为了迭代序列。看起来好像有点使用for循环来做一些特定数量的

次,比如上面。
I know it''s popular and very handy, but I''m curious if there are purists
out there who think that using something like:

for x in range(10):
* * #do something 10 times

is unPythonic. The reason I ask is because the structure of the for loop
seems to be for iterating through a sequence. It seems somewhat
artificial to use the for loop to do something a certain number of
times, like above.



x =范围(10)

打印x


- 输出: -

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

x = range(10)
print x

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


5月11日凌晨3:19,John Salerno< johnj ... @ NOSPAMgmail。 comwrote:
On May 11, 3:19 am, John Salerno <johnj...@NOSPAMgmail.comwrote:

我知道它很受欢迎且非常方便,但我很好奇是否有纯粹主义者

那里谁认为使用类似的东西:


for x in range(10):

#do something 10次


是unPythonic。我问的原因是因为for循环

的结构似乎是为了迭代序列。看起来有点像是b $ b人工使用for循环来做一些特定数量的

次,如上所述。


任何人在那里拒绝以这种方式使用它,或者只是不可能避免

I know it''s popular and very handy, but I''m curious if there are purists
out there who think that using something like:

for x in range(10):
#do something 10 times

is unPythonic. The reason I ask is because the structure of the for loop
seems to be for iterating through a sequence. It seems somewhat
artificial to use the for loop to do something a certain number of
times, like above.

Anyone out there refuse to use it this way, or is it just impossible to
avoid?



嗨John,

如果你有一个既可索引又可迭代的对象,那么

访问每一个成员首先生成索引然后索引

对象是非pythonic。你应该迭代对象。


像大多数规则一样,事情可能会变得边缘化:如果你有每个索引要访问的
对象在一个时间,然后更实用的方法是izip所有对象并迭代结果。

另一种方法是迭代一个枚举对象

并使用创建的索引来索引其他n-1个对象。


在所有这些情况下,你需要记住诸如代码之类的东西/>
清晰度和速度可能会为您做出最终决定。

在下面的示例中,第一个是我将使用的,izip。如果

我需要一个索引然后我更喜欢最后一个,它结合了izip和

枚举:


PythonWin 2.5 (r25:51908,2006年9月19日,09:52:17)[MSC v.1310 32位

(英特尔)]在win32上。

部分版权所有1994-2006 Mark Hammond - 请参阅''帮助/关于PythonWin''

以获取更多版权信息。

Hi John,
If you have an object that is both indexable and iterable, then
visiting every member by first generating an index then indexing the
object is un-pythonic. You should just iterate over the object.

Like most rules, things can get fuzzy around the edges: if you have n
objects to be visited each index at a time, then the more functional
approach would be to izip all the objects and iterate over the result.
Another way would be to iterate over the the enumeration of one object
and use the index created to index the other n-1 objects.

In all such situations you need to remember that things such as code
clarity, and speed, might make the final decision for you.
In the following examples then the first is what I would use, izip. If
I needed an index then I''d prefer the last, which combines izip and
enumerate:

PythonWin 2.5 (r25:51908, Sep 19 2006, 09:52:17) [MSC v.1310 32 bit
(Intel)] on win32.
Portions Copyright 1994-2006 Mark Hammond - see ''Help/About PythonWin''
for further copyright information.


>> obj1 =''CSM''
obj2 =''aue''
obj3 =''tmn''
/> for x,y,z in izip(obj1,obj2,obj3):
>>obj1 = ''CSM''
obj2 = ''aue''
obj3 = ''tmn''
from itertools import izip
for x,y,z in izip(obj1, obj2, obj3):



... print x,y, z

...

C

S um

M en

... print x,y,z
...
C a t
S u m
M e n


>> for i,x in enumerate(obj1):
>>for i,x in enumerate(obj1):



...打印x,obj2 [i],obj3 [i]

...

C at

S um

M en

... print x, obj2[i], obj3[i]
...
C a t
S u m
M e n


>> for i in range(len(obj1)):
>>for i in range(len(obj1)):



... print obj1 [i],obj2 [i],obj3 [i]

。 ..

C

S um

M en

... print obj1[i], obj2[i], obj3[i]
...
C a t
S u m
M e n


>> for i,(x,y,z)枚举(izip(obj1,obj2,obj3)):
>>for i,(x,y,z) in enumerate(izip(obj1, obj2, obj3)):



... print i,x,y,z

...

0 C at

1 S um

2 M en

... print i, x, y, z
...
0 C a t
1 S u m
2 M e n


>>>
>>>



- Paddy。


- Paddy.


这篇关于在for循环中使用range()真的是Pythonic吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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