a sequence question

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

    #16
    Re: a sequence question


    "Nick Coghlan" <ncoghlan@iinet .net.au> wrote in message
    news:mailman.24 34.1108179260.2 2381.python-list@python.org ...[color=blue]
    > A bug report on Sourceforge would help in getting the problem fixed for[/color]
    the 2.5[color=blue]
    > docs[/color]

    Done.

    [color=blue]
    > For the 'left-to-right' evaluation thing, that's technically an[/color]
    implementation[color=blue]
    > artifact of the CPython implementation, since the zip() docs don't make[/color]
    any[color=blue]
    > promises. So updating the docs to include that information would probably[/color]
    be a[color=blue]
    > bigger issue, as it involves behaviour which is currently not defined by[/color]
    the[color=blue]
    > library.[/color]

    OK, thanks.

    Alan Isaac


    Comment

    • Nick Coghlan

      #17
      Re: a sequence question

      David Isaac wrote:[color=blue]
      > "Nick Coghlan" <ncoghlan@iinet .net.au> wrote in message
      > news:mailman.24 34.1108179260.2 2381.python-list@python.org ...
      >[color=green]
      >>A bug report on Sourceforge would help in getting the problem fixed for[/color]
      >
      > the 2.5
      >[color=green]
      >>docs[/color]
      >
      >
      > Done.[/color]

      Bug 1121416, for anyone else interested. Looks Raymond agrees with me about the
      left-to-right evaluation of iterables being an overspecificati on.

      Anyway, that means the zip and izip based solutions are technically version and
      implementation specific. Fortunately, the final versions are fairly easy to turn
      into a custom generator that doesn't rely on izip:

      from itertools import islice, chain, repeat

      def partition(itera ble, part_len):
      itr = iter(iterable)
      while 1:
      item = tuple(islice(it r, part_len))
      if len(item) < part_len:
      raise StopIteration
      yield item

      def padded_partitio n(iterable, part_len, pad_val=None):
      padding = repeat(pad_val, part_len-1)
      itr = chain(iter(iter able), padding)
      return partition(itr, part_len)

      Py> list(partition( range(10), 3))
      [(0, 1, 2), (3, 4, 5), (6, 7, 8)]
      Py> list(padded_par tition(range(10 ), 3))
      [(0, 1, 2), (3, 4, 5), (6, 7, 8), (9, None, None)]
      Py> list(padded_par tition(range(10 ), 3, True))
      [(0, 1, 2), (3, 4, 5), (6, 7, 8), (9, True, True)]

      Well spotted on the fact that the way we were using zip/izip was undocumented,
      btw :)

      Cheers,
      Nick.

      --
      Nick Coghlan | ncoghlan@email. com | Brisbane, Australia
      ---------------------------------------------------------------

      Comment

      Working...