Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #44585
| Path | csiph.com!usenet.pasdenom.info!gegeweb.org!de-l.enfer-du-nord.net!feeder2.enfer-du-nord.net!feeds.phibee-telecom.net!newsfeed.xs4all.nl!newsfeed2.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail |
|---|---|
| Return-Path | <oscar.j.benjamin@gmail.com> |
| X-Original-To | python-list@python.org |
| Delivered-To | python-list@mail.python.org |
| X-Spam-Status | OK 0.002 |
| X-Spam-Evidence | '*H*': 1.00; '*S*': 0.00; 'example:': 0.03; 'subject:not': 0.03; 'argument': 0.05; 'elif': 0.05; 'sized': 0.07; 'arguments': 0.09; 'chunk': 0.09; 'lawrence': 0.09; 'subject:Why': 0.09; 'cc:addr:python-list': 0.11; 'def': 0.12; '3],': 0.16; 'chunks': 0.16; 'function).': 0.16; 'iterator': 0.16; 'itertools': 0.16; 'url:312443': 0.16; 'url:djangoproject': 0.16; 'url:how-do-you-split-a-list-into-evenly-sized-chunks-in-python': 0.16; 'subject:python': 0.16; 'ignore': 0.16; 'wrote:': 0.18; 'split': 0.19; 'python?': 0.22; 'cc:addr:python.org': 0.22; 'error': 0.23; 'cc:2**0': 0.24; 'cc:no real name:2**0': 0.24; 'pass': 0.26; 'post': 0.26; 'values': 0.27; 'header:In-Reply- To:1': 0.27; 'function': 0.29; 'raise': 0.29; 'url:code': 0.29; "doesn't": 0.30; 'message-id:@mail.gmail.com': 0.30; "i'm": 0.30; 'asked': 0.31; "skip:' 10": 0.31; 'extend': 0.32; 'url:python': 0.33; '(e.g.': 0.33; 'cases': 0.33; 'subject:the': 0.34; 'could': 0.34; 'skip:u 20': 0.35; 'something': 0.35; 'received:google.com': 0.35; 'shorter': 0.36; 'yield': 0.36; 'possible': 0.36; 'subject:?': 0.36; 'url:org': 0.36; 'should': 0.36; 'list': 0.37; 'url:library': 0.38; 'issue': 0.38; 'sure': 0.39; 'how': 0.40; 'even': 0.60; 'skip:z 20': 0.60; 'length': 0.61; 'skip:* 10': 0.61; 'simply': 0.61; 'kind': 0.63; 'different': 0.65; 'behavior': 0.77; 'given.': 0.84; 'oscar': 0.84; 'to:addr:yahoo.co.uk': 0.84; 'url:ticket': 0.84; 'mistakes': 0.93; '2013': 0.98 |
| DKIM-Signature | v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=x-received:mime-version:in-reply-to:references:from:date:message-id :subject:to:cc:content-type; bh=wTaQULftU6JHWWJijHS8Y2KUeFinuEiiQ895kOeT2i8=; b=mt77wJJFRwGMKZw9xRBThrNS7IL44yHXsASoMBrWod6gYqxzK5zOjmRHBQ2QFVgGPA g7Uld7CgK7CO4MyNBl67TxyJrdU2ncDVSDxjPXpRPblnEsZvs9AEDLZJimvcUMwXp+em wXHqJ0Syc8KessBcivDJ8Z+8pjsqqo62drgab0wYCAG33ptj2natzjLARM1n94q7Wmb0 QquHNdxN5kntfIeSJ0os/1yT1LJ4xgbk+0kHD/yGNQuHGBUfzpn3fi/yJdxCUmpSV186 z3C1ayLLwZLedv8SqAzecNzMBiIq8Eq+nOu1hgBuB8k3mszLLo2PbHiGbrn3JC867G8r H1ow== |
| X-Received | by 10.220.66.212 with SMTP id o20mr646034vci.2.1367398824891; Wed, 01 May 2013 02:00:24 -0700 (PDT) |
| MIME-Version | 1.0 |
| In-Reply-To | <klqf4g$ekd$1@ger.gmane.org> |
| References | <CAP5i+tYC+cMtseEduTm76LXkaP1Hh-Tz-_HipO7LsURtnuwtOw@mail.gmail.com> <klqf4g$ekd$1@ger.gmane.org> |
| From | Oscar Benjamin <oscar.j.benjamin@gmail.com> |
| Date | Wed, 1 May 2013 10:00:04 +0100 |
| Subject | Re: Why chunks is not part of the python standard lib? |
| To | Mark Lawrence <breamoreboy@yahoo.co.uk> |
| Content-Type | text/plain; charset=ISO-8859-1 |
| Cc | python-list@python.org |
| X-BeenThere | python-list@python.org |
| X-Mailman-Version | 2.1.15 |
| Precedence | list |
| List-Id | General discussion list for the Python programming language <python-list.python.org> |
| List-Unsubscribe | <http://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe> |
| List-Archive | <http://mail.python.org/pipermail/python-list/> |
| List-Post | <mailto:python-list@python.org> |
| List-Help | <mailto:python-list-request@python.org?subject=help> |
| List-Subscribe | <http://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe> |
| Newsgroups | comp.lang.python |
| Message-ID | <mailman.1213.1367398827.3114.python-list@python.org> (permalink) |
| Lines | 69 |
| NNTP-Posting-Host | 2001:888:2000:d::a6 |
| X-Trace | 1367398827 news.xs4all.nl 16005 [2001:888:2000:d::a6]:59051 |
| X-Complaints-To | abuse@xs4all.nl |
| Xref | csiph.com comp.lang.python:44585 |
Show key headers only | View raw
On 1 May 2013 08:10, Mark Lawrence <breamoreboy@yahoo.co.uk> wrote:
> On 01/05/2013 07:26, Ricardo Azpeitia Pimentel wrote:
>>
>> After reading How do you split a list into evenly sized chunks in
>> Python?
>>
>> <http://stackoverflow.com/questions/312443/how-do-you-split-a-list-into-evenly-sized-chunks-in-python>
>>
>> and seeing this kind of mistakes happening
>> https://code.djangoproject.com/ticket/18972 all the time.
>>
>> Why is not a |chunks| function in itertools?
>>
>> |grouper| from
>> http://docs.python.org/2/library/itertools.html#recipes doesn't have the
>> same behavior as |chunks |
>>
>> Example:
>> |
>>
>> |chunks([1, 2, 3, 4, 5], 3)
>> # Should return [[1, 2, 3], [4, 5]] or the iterator equivalent.|
>>
>> |Original Post on StackOverflow:
>>
>> http://stackoverflow.com/questions/16313008/why-chunks-is-not-part-of-the-python-standard-lib
>>
>
> Asked and answered a trillion times. There's no concensus on how chucks
> should behave.
I'm not sure that's a valid argument against it since a chunks
function could just do a different thing depending on the arguments
given.
The issue is around how to deal with the last chunk if it isn't the
same length as the others and I can only think of 4 reasonable
responses:
1) Yield a shorter chunk
2) Extend the chunk with fill values
3) Raise an error
4) Ignore the last chunk
Cases 2 and 4 can be achieved with current itertools primitives e.g.:
2) izip_longest(fillvalue=fillvalue, *[iter(iterable)] * n)
4) zip(*[iter(iterable)] * n)
However I have only ever had use cases for 1 and 3 and these are not
currently possible without something additional (e.g. a generator
function).
In any case a chunks function can simply take arguments to give all 4
behaviours:
def chunks(iterable, chunksize, uneven='return_short', fillvalue=None):
# loop through yielding all even chunks
# and then
if uneven == 'return_short:
yield chunk
elif uneven == 'raise':
raise ValueError('No items left')
elif uneven == 'fill':
yield chunk + [fillvalue] * (chunksize - len(chunk))
elif uneven == 'ignore':
pass
Oscar
Back to comp.lang.python | Previous | Next — Next in thread | Find similar | Unroll thread
Re: Why chunks is not part of the python standard lib? Oscar Benjamin <oscar.j.benjamin@gmail.com> - 2013-05-01 10:00 +0100
Re: Why chunks is not part of the python standard lib? Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2013-05-02 05:15 +0000
Re: Why chunks is not part of the python standard lib? Wolfgang Maier <wolfgang.maier@biologie.uni-freiburg.de> - 2013-05-02 08:53 +0000
Re: Why chunks is not part of the python standard lib? Mark Lawrence <breamoreboy@yahoo.co.uk> - 2013-05-02 10:23 +0100
Re: Why chunks is not part of the python standard lib? Oscar Benjamin <oscar.j.benjamin@gmail.com> - 2013-05-02 13:52 +0100
Re: Why chunks is not part of the python standard lib? Chris Angelico <rosuav@gmail.com> - 2013-05-02 22:55 +1000
Re: Why chunks is not part of the python standard lib? Oscar Benjamin <oscar.j.benjamin@gmail.com> - 2013-05-02 14:23 +0100
Re: Why chunks is not part of the python standard lib? Wolfgang Maier <wolfgang.maier@biologie.uni-freiburg.de> - 2013-05-02 14:02 +0000
csiph-web