Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #104832

Re: Simple exercise

From Peter Otten <__peter__@web.de>
Newsgroups comp.lang.python
Subject Re: Simple exercise
Date 2016-03-14 17:00 +0100
Organization None
Message-ID <mailman.112.1457971293.12893.python-list@python.org> (permalink)
References (3 earlier) <nbt7qr$4fa$1@dont-email.me> <mailman.167.1457661831.15725.python-list@python.org> <88c5b5fa-66a0-461a-8ae4-b3264b32f679@googlegroups.com> <CAHVvXxTAy=Ey7-+eLWwyjZXwwOn0_ay-i5-=trTRBQpKE19zZw@mail.gmail.com> <CALwzidmSxfhH6RBxk33A6=zqvtwhj5nXi_eh=0uKvuX2E=x49A@mail.gmail.com>

Show all headers | View raw


Ian Kelly wrote:

> On Mon, Mar 14, 2016 at 9:06 AM, Oscar Benjamin
> <oscar.j.benjamin@gmail.com> wrote:
>> On 14 March 2016 at 14:35, Rick Johnson <rantingrickjohnson@gmail.com>
>> wrote:
>>>
>>> I would strongly warn anyone against using the zip function
>>> unless
>> ...
>>> I meant to say: absolutely, one hundred percent *SURE*, that
>>> both sequences are of the same length, or, absolutely one
>>> hundred percent *SURE*, that dropping values is not going to
>>> matter. For that reason, i avoid the zip function like the
>>> plague. I would much rather get an index error, than let an
>>> error pass silently.
>>
>> I also think it's unfortunate that zip silently discards items. Almost
>> always when I use zip I would prefer to see an error when the two
>> iterables are not of the same length.
> 
> It's sometimes very useful, though. For example on multiple occasions
> I've taken advantage of the fact that enumerate(x) is equivalent to
> zip(itertools.count(), x). If zip raised an error then that would only
> be possible using islice, and then only if the length is known in
> advance.
> 
> Also, in order for zip to know that the lengths are not equal, it
> would have to try to read one additional item from the longer
> iterable. That's rather unfortunate if it's an iterator and you're
> hoping to catch the exception and then use the rest of the iterator
> for something else.

That's a problem you may run into with the current zip(), too -- unless you 
know that the first is the shortest iterator:

>>> from itertools import count
>>> indices = count()
>>> for items in "abc", "def", "gh":
...    list(zip(indices, items))
... 
[(0, 'a'), (1, 'b'), (2, 'c')]
[(4, 'd'), (5, 'e'), (6, 'f')]
[(8, 'g'), (9, 'h')]

I'm with Oscar here, raising an exception would be the better default; the 
current implementation could have been made available as 
itertools.zip_shortest().

Back to comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

Simple exercise Rodrick Brown <rodrick.brown@gmail.com> - 2016-03-10 04:02 -0500
  Re: Simple exercise Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2016-03-10 11:30 +0100
    Re: Simple exercise Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2016-03-10 12:07 +0100
    Re: Simple exercise Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2016-03-10 17:05 +0100
      Re: Simple exercise Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2016-03-10 17:08 +0100
  Re: Simple exercise Gregory Ewing <greg.ewing@canterbury.ac.nz> - 2016-03-11 12:24 +1300
    Re: Simple exercise Chris Angelico <rosuav@gmail.com> - 2016-03-11 10:38 +1100
  Re: Simple exercise BartC <bc@freeuk.com> - 2016-03-11 00:05 +0000
    Re: Simple exercise Mark Lawrence <breamoreboy@yahoo.co.uk> - 2016-03-11 01:21 +0000
      Re: Simple exercise BartC <bc@freeuk.com> - 2016-03-11 01:45 +0000
        Re: Simple exercise Larry Martell <larry.martell@gmail.com> - 2016-03-10 20:53 -0500
        Re: Simple exercise "Martin A. Brown" <martin@linux-ip.net> - 2016-03-10 17:56 -0800
        Re: Simple exercise Mark Lawrence <breamoreboy@yahoo.co.uk> - 2016-03-11 02:03 +0000
          Re: Simple exercise BartC <bc@freeuk.com> - 2016-03-11 02:18 +0000
          Re: Simple exercise Rick Johnson <rantingrickjohnson@gmail.com> - 2016-03-14 07:35 -0700
            Re: Simple exercise Oscar Benjamin <oscar.j.benjamin@gmail.com> - 2016-03-14 15:06 +0000
              Re: Simple exercise Rick Johnson <rantingrickjohnson@gmail.com> - 2016-03-14 09:00 -0700
              Re: Simple exercise Steven D'Aprano <steve@pearwood.info> - 2016-03-15 10:59 +1100
                Re: Simple exercise Jussi Piitulainen <jussi.piitulainen@helsinki.fi> - 2016-03-15 07:26 +0200
                Re: Simple exercise Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2016-03-15 19:39 +1100
                Re: Simple exercise Chris Angelico <rosuav@gmail.com> - 2016-03-15 19:53 +1100
                Re: Simple exercise Jussi Piitulainen <jussi.piitulainen@helsinki.fi> - 2016-03-15 11:04 +0200
                Re: Simple exercise Oscar Benjamin <oscar.j.benjamin@gmail.com> - 2016-03-15 11:09 +0000
            Re: Simple exercise Ian Kelly <ian.g.kelly@gmail.com> - 2016-03-14 09:16 -0600
              Re: Simple exercise Rick Johnson <rantingrickjohnson@gmail.com> - 2016-03-14 09:11 -0700
            Re: Simple exercise Mark Lawrence <breamoreboy@yahoo.co.uk> - 2016-03-14 15:23 +0000
            Re: Simple exercise Peter Otten <__peter__@web.de> - 2016-03-14 17:00 +0100
        Re: Simple exercise Mark Lawrence <breamoreboy@yahoo.co.uk> - 2016-03-11 02:05 +0000
      Re: Simple exercise Rick Johnson <rantingrickjohnson@gmail.com> - 2016-03-14 07:07 -0700
        Re: Simple exercise Larry Martell <larry.martell@gmail.com> - 2016-03-14 10:13 -0400
        Re: Simple exercise alister <alister.ware@ntlworld.com> - 2016-03-14 14:18 +0000
          Re: Simple exercise Rick Johnson <rantingrickjohnson@gmail.com> - 2016-03-14 08:22 -0700
            Re: Simple exercise MRAB <python@mrabarnett.plus.com> - 2016-03-14 15:57 +0000
    Re: Simple exercise Chris Kaynor <ckaynor@zindagigames.com> - 2016-03-10 18:14 -0800
  Re: Simple exercise boffi <boffi@casa.sua> - 2016-03-17 22:28 +0100

csiph-web