为什么我不喜欢range / xrange [英] why I don't like range/xrange

查看:71
本文介绍了为什么我不喜欢range / xrange的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好!


很多时候我被建议使用xrange和range而不是

构造,实际上,它们更优雅 - 但是,在使用

range / xrange和while循环来计算开销(并且放松灵活性)之后,你得出的结论是它不是

非常值得使用范围/ xrange循环。


我想展示一些例子,如果有人能建议,我会很高兴>
一些其他修正比一个循环:-)


a)范围overfllow:

for i in range(0,1<< ; len(S)):

......

溢出错误:range()结果有太多项目

$ b $好的,所以我们用xrange解决这个问题!


b)xrange long int溢出:

$ x $ b for x in xrange(0,1< ;< len(S)):

.........

OverflowError:long int太大而无法转换为int


接下来我想念的是flexi C for循环中的bility:


for(i = 0; some_function()/ *或其他条件* /; i ++)


或,


for(i = 0; i< 10; i ++)

i = 10;

我不认为range / xrange很糟糕,但我认为应该有一些其他结构可以提高循环灵活性。其他的东西

可能是,我只是错过了一个同样优雅的选择,这就是为什么我会听到一些关于如何解决上述问题的建议..(btw,

我已经浏览了与我的问题相关的档案,但我没有看到

任何好的解决方案)
< br $>
谢谢


Jernej。

Hello!

Many times I was suggested to use xrange and range instead of the
while constructs, and indeed, they are quite more elegant - but, after
calculating the overhead (and losen flexibility) when working with
range/xrange, and while loops, you get to the conclusion that it isn''t
really worth using range/xrange loops.

I''d like to show some examples and I''ll be glad if someone can suggest
some other fixes than while a loop :-)

a) range overfllow :
for i in range(0, 1 << len(S)) :
......
OverflowError: range() result has too many items

ok, so we fix this one with xrange !

b) xrange long int overflow :

for i in xrange(0, 1 << len(S)) :
.........
OverflowError: long int too large to convert to int

Next thing I miss is the flexibility as in C for loops :

for (i = 0; some_function() /* or other condition */ ; i++)

or,

for (i = 0 ; i < 10 ; i++)
i = 10;
I don''t think range/xrange sucks, but I really think there should be
some other constructs to improve the looping flexibility. Other thing
may be, that I just miss an equally elegant alternative that''s why I''d
like to hear some suggestions on how to fix the above issues.. (btw,
I''ve already browsed the archives related to my issue,but i don''t see
any good solution)

Thanks

Jernej.

推荐答案

2月16日2007 07:30:15 -0800,stdazi< st **** @ gmail.comwrote:
On 16 Feb 2007 07:30:15 -0800, stdazi <st****@gmail.comwrote:

你好!


很多时候我建议使用xrange和range而不是

构造,实际上,它们更优雅 - 但是,在
计算开销之后(使用

range / xrange和while循环时,你可以得出结论:使用range / xrange循环确实不值得b
$ b。


我想展示一些例子,如果是的话,我会很高兴omeone可以建议

一些其他修复,而不是循环:-)


a)范围overfllow:


for i在范围内(0,1 << len(S)):

.....

溢出错误:range()结果有太多项目


ok ,所以我们用xrange解决这个问题!


b)xrange long int溢出:

$ x $ b for x in xrange(0,1<< ; len(S)):

........

OverflowError:long int太大而无法转换为int
Hello!

Many times I was suggested to use xrange and range instead of the
while constructs, and indeed, they are quite more elegant - but, after
calculating the overhead (and losen flexibility) when working with
range/xrange, and while loops, you get to the conclusion that it isn''t
really worth using range/xrange loops.

I''d like to show some examples and I''ll be glad if someone can suggest
some other fixes than while a loop :-)

a) range overfllow :
for i in range(0, 1 << len(S)) :
.....
OverflowError: range() result has too many items

ok, so we fix this one with xrange !

b) xrange long int overflow :

for i in xrange(0, 1 << len(S)) :
........
OverflowError: long int too large to convert to int



xrange应该能够处理这个问题。它可能值得一个错误报告。

xrange should be able to handle this. It''s probably worth a bug report.


接下来我想念的是C for for循环的灵活性:


for(i = 0; some_function()/ *或其他条件* /; i ++)
Next thing I miss is the flexibility as in C for loops :

for (i = 0; some_function() /* or other condition */ ; i++)



你在这里使用常规for或while循环,没有循环索引。

You''d use a regular for or a while loop here, without the loop index.


或,


for(i = 0; i< 10; i ++)

i = 10;
or,

for (i = 0 ; i < 10 ; i++)
i = 10;



这不会像写的那样做。对于它的所有合理用途,

你可以用相同的方式在Python中完成它。用Python编写C代码是浪费时间和挫折的练习。

This doesn''t do anything as written. For all reasonable uses of it,
you can do it the same way in Python. Writing C code in Python is a
waste of time and an exercise in frustration.


>

我不认为range / xrange很糟糕,但我真的认为应该有一些其他结构来改善循环灵活性。其他的东西

可能是,我只是错过了一个同样优雅的选择,这就是为什么我会听到一些关于如何解决上述问题的建议..(btw,

我已经浏览了与我的问题相关的档案,但我没有看到

任何好的解决方案)
>
I don''t think range/xrange sucks, but I really think there should be
some other constructs to improve the looping flexibility. Other thing
may be, that I just miss an equally elegant alternative that''s why I''d
like to hear some suggestions on how to fix the above issues.. (btw,
I''ve already browsed the archives related to my issue,but i don''t see
any good solution)



在Python中很少有range()和xrange()的枚举。循环如:


data = [...]
$ x $ b for x in xrange(len(data)):

datum =数据[ii]


是疣。枚举你将要使用的任何东西。


在某些情况下,你需要索引和项目。使用枚举:


索引,枚举数据(数据):

....

Enumeration over range() and xrange() is rare in Python. loops like:

data = [...]
for ii in xrange(len(data)):
datum = data[ii]

are a wart. Enumerate over whatever you''re going to work with.

In some cases, you want the index and the item. Use enumerate for that:

for index, datum in enumerate(data):
....

2月16日上午9:30,stdazi < std ... @ gmail.comwrote:
On Feb 16, 9:30 am, "stdazi" <std...@gmail.comwrote:

你好!


很多次我被建议使用xrange和范围而不是

虽然构造,实际上,它们更优雅 - 但是,在使用

计算开销(和灵活性)后使用

range / xrange,而while循环,你得出的结论是它不是
真的值得使用range / xrange循环。


我想展示一些例子,如果有人可以推荐一些其他的补丁而不是循环,我会很高兴:-)


a)范围超限:


我在范围内(0,1 << len(S)):

.....

OverflowError:range()结果有太多项目


ok,所以我们用xrange解决这个问题!


b )xrange long int overflow:

$ x $ b for x in xrange(0,1<< len(S)):

...... ..

OverflowError:long int too l arge转换为int


接下来我想念的是C for循环中的灵活性:


for(i = 0; some_function()/ *或其他条件* /; i ++)


或,


for(i = 0; i< 10; i ++)

i = 10;


我不认为range / xrange很糟糕,但我认为应该有一些其他结构来改善循环灵活性。其他的东西

可能是,我只是错过了一个同样优雅的选择,这就是为什么我会听到一些关于如何解决上述问题的建议..(btw,

我已经浏览了与我的问题相关的档案,但我没有看到

任何好的解决方案)


谢谢


Jernej。
Hello!

Many times I was suggested to use xrange and range instead of the
while constructs, and indeed, they are quite more elegant - but, after
calculating the overhead (and losen flexibility) when working with
range/xrange, and while loops, you get to the conclusion that it isn''t
really worth using range/xrange loops.

I''d like to show some examples and I''ll be glad if someone can suggest
some other fixes than while a loop :-)

a) range overfllow :

for i in range(0, 1 << len(S)) :
.....
OverflowError: range() result has too many items

ok, so we fix this one with xrange !

b) xrange long int overflow :

for i in xrange(0, 1 << len(S)) :
........
OverflowError: long int too large to convert to int

Next thing I miss is the flexibility as in C for loops :

for (i = 0; some_function() /* or other condition */ ; i++)

or,

for (i = 0 ; i < 10 ; i++)
i = 10;

I don''t think range/xrange sucks, but I really think there should be
some other constructs to improve the looping flexibility. Other thing
may be, that I just miss an equally elegant alternative that''s why I''d
like to hear some suggestions on how to fix the above issues.. (btw,
I''ve already browsed the archives related to my issue,but i don''t see
any good solution)

Thanks

Jernej.



我自己的代码很少使用范围或xrange,我的大部分for循环

迭代序列或生成器,如 ;对于blahList中的项目:执行

带项目的东西。我认为range / xrange是常见的初学者'
构造,因为它们反映了更像C的循环方法(对于我在
范围内的i)(len(blahList) )):用blahList [i]"做一些事情。


我真的不会*鼓励使用range / xrange,但感觉

迭代序列和生成器是更优雅/ Pythonic

的方式 - 谁建议你使用range / xrange?


就此而言,只是什么你打算做2 ** len(S)次?如果S $ / b $ b b具有任何显着的长度,那么在你完成b $ b之前,太阳可能是一团煤,无论你使用什么循环机制(尽管它

有意义避免范围'创建

2 ** len(S)项目的列表 - 幸运的是,实施已经解决了这个

问题是通过提出那个'太多的项目异常,谢天谢地

so)。


也许不是在范围/ xrange附近工作,你应该考虑是否首先对你的问题采取的方法是否可行。


- 保罗

Very little of my own code uses range or xrange, most of my for loops
iterate over a sequence or generator, as in "for item in blahList: do
something with item". I think range/xrange are common beginner''s
constructs, since they reflect a more C-like looping method ("for i in
range(len(blahList)): do something with blahList[i]").

I really would *not* encourage use of range/xrange, but feel that
iteration over sequences and generators is the more elegant/Pythonic
way to go - who is suggesting you use range/xrange?

For that matter, just what is it you plan to do 2**len(S) times? If S
is of any significant length, the sun may be a lump of coal before you
are finished, regardless of what loop mechanism you use (although it
would make sense to avoid range''s implementation of creating a list of
2**len(S) items - fortunately the implementation already resolves this
problem by raising a "that''s too many items" exception, and thankfully
so).

Maybe instead of working around range/xrange, you should think whether
a 2**len(S) approach to your problem is feasible in the first place.

-- Paul


stdazi写道:
stdazi wrote:

你好!


多次我被建议使用xrange和range而不是

构造,实际上,它们更优雅 - 但是,在

之后计算开销(和灵活性)使用
range / xrange,而while循环,你得出的结论是,使用range / xrange循环确实不值得



我想展示一些例子,如果有人可以推荐其他一些补丁而不是循环,我会很高兴:-)


a)范围overfllow:


for i in range(0,1<< len(S)):

.....

溢出错误:range()结果有太多项目


ok ,所以我们用xrange解决这个问题!


b)xrange long int溢出:

$ x $ b for x in xrange(0,1<< ; len(S)):

........

OverflowError:long int太大而无法转换为int


接下来我想念的是C for循环中的灵活性:


for(i = 0; some_function()/ *或其他条件* /; i ++)


或,


for(i = 0; i< 10; i ++)

i = 10;


我不认为range / xrange很糟糕,但我认为应该有一些其他结构来提高循环灵活性。其他的东西

可能是,我只是错过了一个同样优雅的选择,这就是为什么我会听到一些关于如何解决上述问题的建议..(btw,

我已经浏览了与我的问题相关的档案,但我没有看到

任何好的解决方案)
< br $>
谢谢


Jernej。
Hello!

Many times I was suggested to use xrange and range instead of the
while constructs, and indeed, they are quite more elegant - but, after
calculating the overhead (and losen flexibility) when working with
range/xrange, and while loops, you get to the conclusion that it isn''t
really worth using range/xrange loops.

I''d like to show some examples and I''ll be glad if someone can suggest
some other fixes than while a loop :-)

a) range overfllow :
for i in range(0, 1 << len(S)) :
.....
OverflowError: range() result has too many items

ok, so we fix this one with xrange !

b) xrange long int overflow :

for i in xrange(0, 1 << len(S)) :
........
OverflowError: long int too large to convert to int

Next thing I miss is the flexibility as in C for loops :

for (i = 0; some_function() /* or other condition */ ; i++)

or,

for (i = 0 ; i < 10 ; i++)
i = 10;
I don''t think range/xrange sucks, but I really think there should be
some other constructs to improve the looping flexibility. Other thing
may be, that I just miss an equally elegant alternative that''s why I''d
like to hear some suggestions on how to fix the above issues.. (btw,
I''ve already browsed the archives related to my issue,but i don''t see
any good solution)

Thanks

Jernej.



你在xrange中的i的例子(0,1< < len(s)):必须导致

a数超过10亿。你真的这么做了吗?

你刚才指出一个边缘案例吗?


你可以随时使用while循环:


i = 0

而i< (1<< len(s)):

i + = 1




i = 1

而1:

如果我= = 10:休息

i + = 1


我个人经常使用很多for循环在我的代码中我迭代

在列表上或者用生成器作为我的目标返回实例

我的数据直到用尽并找到易于阅读的代码:


for myfile中的行:

#用行做点什么


甚至不需要思考关于数字,除非我想保留

跟踪行号,我会这样做:


for linenumber,line in enumerate(myfile):

#对每个亚麻布做一些事情,一行


我发现我使用i,j,k指针越来越少,因为我在
Python。我想这正是你习惯使用的。


-Larry

Your example of for i in xrange(0, 1<<len(s)): must have resulted in
a number greater than 1 billion. Are you really doing this much or
are you just pointing out an edge case here?

You can always use while loop:

i=0
while i < (1<<len(s)):
i+=1
or

i=1
while 1:
if i == 10: break
i+=1

Personally I use a lot of for loops in my code where I iterate
over lists or with a generator as my target that returns instances
of my data until exhausted and find the code easy to read:

for line in myfile:
# do something with the line

No need to even "think" about numbers here unless I want to keep
track of the line numbers in which case I would do:

for linenumber, line in enumerate(myfile):
# do something with each linenumber, line

I find that I use i, j, k pointers less and less as I write more code in
Python. I guess it is just what you get accustomed to using.

-Larry


这篇关于为什么我不喜欢range / xrange的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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