434,959 Members | 2,432 Online
Need help? Post your question and get tips & solutions from a community of 434,959 IT Pros & Developers. It's quick & easy.

# what's the python for this C statement?

 P: n/a 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) which is equivalent to: for (i = 0; i < 10; i++) However, how this C statement will be translated in Python? for (j = i = 0; i < (1 << H); i++) Thanks Oct 20 '08 #1
6 Replies

 P: n/a On Mon, Oct 20, 2008 at 2:56 AM, Michele which is equivalent to: for (i = 0; i < 10; i++) However, how this C statement will be translated in Python? for (j = i = 0; i < (1 << H); i++) There's no nice way to translate more complex 'for' statements like that (unless you can calculate the end value somehow). You basically just have to use a 'while' loop instead: i = j = 0 while i < 1 << H: #body goes here i += 1 Since bit-shifting doesn't get used much in Python and iteration through a generator or the items of a collection is more common, this doesn't end up being a problem in practice. Cheers, Chris -- Follow the path of the Iguana... http://rebertia.com > Thanks -- http://mail.python.org/mailman/listinfo/python-list Oct 20 '08 #2

 P: n/a Michele

 P: n/a On Mon, 20 Oct 2008 12:34:11 +0200, Hrvoje Niksic wrote: Michele Hi there,I'm relative new to Python and I discovered that there's one single wayto 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. Oct 20 '08 #4

 P: n/a Lie Ryan wrote: (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. In 3.0, range is a class and range(arg) is a re-iterable instance of that class. >>a = range(10,2,-3)a range(10, 2, -3) >>list(a) [10, 7, 4] >>list(a) [10, 7, 4] Re-iterablility is handy if you want to iterate twice over the same range, especially is the range object is computed elsewhere. Map and filter, on the other hand, produce one-use iterators. Filter takes one iterable as input and map takes many iterables. Both make no presumption that the inputs are re-iterable rather than a one-time iterators. Terry Jan Reedy Oct 20 '08 #5

 P: n/a On Mon, 20 Oct 2008 17:01:13 +0000, Lie Ryan wrote: On Mon, 20 Oct 2008 12:34:11 +0200, Hrvoje Niksic wrote: >Michele >Hi there,I'm relative new to Python and I discovered that there's one singleway 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). That hasn't been true for some time now: >>timeit.Timer('range(3)').repeat() [1.8658630847930908, 0.89470076560974121, 0.88842916488647461] >>timeit.Timer('xrange(3)').repeat() [0.71410012245178223, 0.69949698448181152, 0.69640421867370605] That's on Python 2.5. -- Steven Oct 20 '08 #6

 P: n/a Lie Ryan Michele >Hi there,I'm relative new to Python and I discovered that there's one single wayto 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). This used to be the case, but is no longer true as of (I think) Python 2.4. xrange is preferred over range, unless you actually need the list, of course. You are right that it is changing, but that is in Python 3, which only has range in the meaning of today's xrange. \$ python -m timeit 'for i in xrange(10): pass' 1000000 loops, best of 3: 1.13 usec per loop \$ python -m timeit 'for i in range(10): pass' 1000000 loops, best of 3: 1.72 usec per loop Oct 21 '08 #7

### This discussion thread is closed

Replies have been disabled for this discussion.