替代标准C“for”;

编程入门 行业动态 更新时间:2024-10-26 11:29:34
本文介绍了替代标准C“for”;的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

你好, 我对Python很陌生,并且有一个直接的&简单的问题。 在C中,有(init; cond; advance)。我们都知道。 在Python中有两种循环方式i = A..B(数字。): 1)i = A 而我< B: ......做点什么... i + = STEP 2)for i in range (A,B,STEP): ...做点什么... 第一个案子看起来很讨厌,因为它更复杂 东西,不是数字循环。第二个是非常好的,但有一个问题,那就是b $ b问题。如果我做..范围(1,100000000)..(我真的需要 有时),它需要几百兆的内存并减慢 下来。这个简单问题还有其他好的方法吗?发电机? Adomas

Hi there, I am quite new to Python, and have a straight & simple question. In C, there is for (init; cond; advance). We all know that. In Python there are two ways to loop over i=A..B (numerical.): 1) i = A while i<B: ...do something... i+=STEP 2) for i in range(A, B, STEP): ...do something... First case looks quite nasty, because it''s for more complicated things, not numerical loops. Second is very nice, but with there''s problem. If i do ..in range(1, 100000000).. (what I really need sometimes), it takes few hundred megs of memory and slows down. Are there other good ways for this simple problem? Generators? Adomas

推荐答案

广告****************** @ gmail 写道: 问题。如果我做..在范围内(1,100000000)..(有时我真正需要的是什么),它需要几百兆的内存并且慢下来。这个简单问题还有其他好的方法吗?发生器? problem. If i do ..in range(1, 100000000).. (what I really need sometimes), it takes few hundred megs of memory and slows down. Are there other good ways for this simple problem? Generators?

使用xrange而不是range。

use xrange instead of range.

>我是Python的新手,并且有一个直接的&简单的问题。 > I am quite new to Python, and have a straight & simple question. 在C中,有(init; cond; advance)。我们都知道。在Python中有两种方法可以循环i = A..B(数字。): 1)i = A 而i< B: ......做点什么...... 我+ = STEP In C, there is for (init; cond; advance). We all know that. In Python there are two ways to loop over i=A..B (numerical.): 1) i = A while i<B: ...do something... i+=STEP

这确实很难看。你很少需要在Python中使用这样的循环和 ,并且有些人认为你可以经常将C等价翻译为 更多pythonic。正如你猜测的那样,你的第二个问题是最好用生成器函数解决 - xrange()。它完全等于 range(),除了它返回一个生成器而不是一个列表。 - mvh Bj?rn

This is indeed quite ugly. You rarely need such loops in Python and with some thinking you can often translate the C-equivalent to something more pythonic. As you guessed, your second problem is best solved with a generator function - xrange(). It is completely equal to range() except that it returns a generator instead of a list. -- mvh Bj?rn

ad *** ***************@gmail 写道: 还有其他好方法可以解决这个简单问题吗?发生器? Are there other good ways for this simple problem? Generators?

非常有趣的问题:)从来没有发生在我身上。 防止python将整个列表加载到内存,一个 可以,正如你的建议,使用一个生成器:

Very interesting problem :) That never occured to me. To prevent python from loading that entire list into memory, one could, as you suggested, use a generator:

def genrange(start,stop,step = 1): while start<停止: 收益率开始 开始+ =步骤 for x in range(5): print"%s" ; %str(x), 0 1 2 3 4 for gen in genrange(0,5): def genrange( start , stop , step = 1 ): while start < stop: yield start start += step for x in range( 5 ): print "%s " % str( x ), 0 1 2 3 4 for x in genrange( 0 , 5 ):

print"%s" %str(x), 0 1 2 3 4 - Daniel Bickett dbickett at gmail heureusement/

更多推荐

替代标准C“for”;

本文发布于:2023-11-15 17:24:51,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1596470.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:标准

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!