what's the python for this C statement?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Michele

    #1

    what's the python for this C statement?

    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
  • Chris Rebert

    #2
    Re: what's the python for this C statement?

    On Mon, Oct 20, 2008 at 2:56 AM, Michele <michele@nectar ine.itwrote:
    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)
    Actually, you want:

    for i in range(10):

    Since starting at 0 and using a step of 1 are the defaults.
    >
    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...


    Comment

    • Hrvoje Niksic

      #3
      Re: what's the python for this C statement?

      Michele <michele@nectar ine.itwrites:
      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.
      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.

      Comment

      • Lie Ryan

        #4
        Re: what's the python for this C statement?

        On Mon, 20 Oct 2008 12:34:11 +0200, Hrvoje Niksic wrote:
        Michele <michele@nectar ine.itwrites:
        >
        >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.

        Comment

        • Terry Reedy

          #5
          Re: what's the python for this C statement?

          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

          Comment

          • Steven D'Aprano

            #6
            Re: what's the python for this C statement?

            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 <michele@nectar ine.itwrites:
            >>
            >>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).

            That hasn't been true for some time now:
            >>timeit.Timer( 'range(3)').rep eat()
            [1.8658630847930 908, 0.8947007656097 4121, 0.8884291648864 7461]
            >>timeit.Timer( 'xrange(3)').re peat()
            [0.7141001224517 8223, 0.6994969844818 1152, 0.6964042186737 0605]


            That's on Python 2.5.



            --
            Steven

            Comment

            • Hrvoje Niksic

              #7
              Re: what's the python for this C statement?

              Lie Ryan <lie.1296@gmail .comwrites:
              On Mon, 20 Oct 2008 12:34:11 +0200, Hrvoje Niksic wrote:
              >
              >Michele <michele@nectar ine.itwrites:
              >>
              >>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).
              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

              Comment

              Working...