what's the python for this C statement?

Lie Ryan lie.1296 at gmail.com
Mon Oct 20 13:01:13 EDT 2008


On Mon, 20 Oct 2008 12:34:11 +0200, Hrvoje Niksic wrote:

> Michele <michele at nectarine.it> writes:
> 
>> Hi there,
>> I'm relative new to Python and I discovered that there's one single way
>> to cycle over an integer variable with for: for i in range(0,10,1)
> 
> Please use xrange for this purpose, especially with larger iterations. 
> range actually allocates a sequence.

Actually that doesn't really matter unless the loop is extremely big (> a 
million), since range is much faster than xrange for smaller values 
(which might be the more typical case). And I think range will be an 
iterator in the future, imitating the behavior of xrange. So it doesn't 
really matter anyway.

>> However, how this C statement will be translated in Python?
>>
>> for (j = i = 0; i < (1 << H); i++)
> 
> If H doesn't change during the loop, you can use:
> 
> j = 0
> for i in xrange(1 << H):
>     ...
> 
> If H can change, simply rewrite it into an obvious 'while' loop.

I would suggest a different thing: rewrite it in python instead of 
translating it.




More information about the Python-list mailing list