2月16日上午9:30,stdazi < std ... @ gmail.comwrote:On Feb 16, 9:30 am, "stdazi" <[email protected]: 你好! 很多次我被建议使用xrange和范围而不是 虽然构造,实际上,它们更优雅 - 但是,在使用 计算开销(和灵活性)后使用 range / xrange,而while循环,你得出的结论是它不是真的值得使用range / xrange循环。 我想展示一些例子,如果有人可以推荐一些其他的补丁而不是循环,我会很高兴:-) a)范围超限: 我在范围内(0,1 ..... 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 thewhile constructs, and indeed, they are quite more elegant - but, aftercalculating the overhead (and losen flexibility) when working withrange/xrange, and while loops, you get to the conclusion that it isn''treally worth using range/xrange loops.I''d like to show some examples and I''ll be glad if someone can suggestsome other fixes than while a loop :-)a) range overfllow :for i in range(0, 1 << len(S)) :.....OverflowError: range() result has too many itemsok, 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 intNext 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 besome other constructs to improve the looping flexibility. Other thingmay be, that I just miss an equally elegant alternative that''s why I''dlike 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 seeany good solution)ThanksJernej. 我自己的代码很少使用范围或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 loopsiterate over a sequence or generator, as in "for item in blahList: dosomething with item". I think range/xrange are common beginner''sconstructs, since they reflect a more C-like looping method ("for i inrange(len(blahList)): do something with blahList[i]").I really would *not* encourage use of range/xrange, but feel thatiteration over sequences and generators is the more elegant/Pythonicway 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 Sis of any significant length, the sun may be a lump of coal before youare finished, regardless of what loop mechanism you use (although itwould make sense to avoid range''s implementation of creating a list of2**len(S) items - fortunately the implementation already resolves thisproblem by raising a "that''s too many items" exception, and thankfullyso).Maybe instead of working around range/xrange, you should think whethera 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, 我已经浏览了与我的问题相关的档案,但我没有看到 任何好的解决方案) 谢谢 Jernej。Hello!Many times I was suggested to use xrange and range instead of thewhile constructs, and indeed, they are quite more elegant - but, aftercalculating the overhead (and losen flexibility) when working withrange/xrange, and while loops, you get to the conclusion that it isn''treally worth using range/xrange loops.I''d like to show some examples and I''ll be glad if someone can suggestsome other fixes than while a loop :-)a) range overfllow :for i in range(0, 1 << len(S)) :.....OverflowError: range() result has too many itemsok, 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 intNext 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 besome other constructs to improve the looping flexibility. Other thingmay be, that I just miss an equally elegant alternative that''s why I''dlike 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 seeany good solution)ThanksJernej. 你在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。我想这正是你习惯使用的。 -LarryYour example of for i in xrange(0, 1<<len(s)): must have resulted ina number greater than 1 billion. Are you really doing this much orare you just pointing out an edge case here?You can always use while loop:i=0while i < (1<<len(s)):i+=1ori=1while 1:if i == 10: breaki+=1Personally I use a lot of for loops in my code where I iterateover lists or with a generator as my target that returns instancesof my data until exhausted and find the code easy to read:for line in myfile:# do something with the lineNo need to even "think" about numbers here unless I want to keeptrack of the line numbers in which case I would do:for linenumber, line in enumerate(myfile):# do something with each linenumber, lineI find that I use i, j, k pointers less and less as I write more code inPython. I guess it is just what you get accustomed to using.-Larry 这篇关于为什么我不喜欢range / xrange的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 1403页,肝出来的..
09-06 13:36