Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #41987
| Path | csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!feeder.erje.net!eu.feeder.erje.net!newsfeed.freenet.ag!news2.euro.net!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail |
|---|---|
| Return-Path | <python-python-list@m.gmane.org> |
| X-Original-To | python-list@python.org |
| Delivered-To | python-list@mail.python.org |
| X-Spam-Status | OK 0.001 |
| X-Spam-Evidence | '*H*': 1.00; '*S*': 0.00; 'skip:[ 20': 0.04; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'subject:into': 0.09; "'b',": 0.16; "'c',": 0.16; "'d',": 0.16; "'e',": 0.16; "['a',": 0.16; 'chunks.': 0.16; 'itertools': 0.16; 'length,': 0.16; 'range(0,': 0.16; 'received:80.91.229.3': 0.16; 'received:dip.t-dialin.net': 0.16; 'received:plane.gmane.org': 0.16; 'received:t-dialin.net': 0.16; 'slicing:': 0.16; 'subject:python': 0.16; 'wrote:': 0.18; 'obviously': 0.18; 'split': 0.19; '>>>': 0.22; 'print': 0.22; 'header:User-Agent:1': 0.23; 'second': 0.26; 'header:X-Complaints-To:1': 0.27; 'wondering': 0.29; 'subject:list': 0.30; 'subject:size': 0.31; 'work:': 0.31; 'anyone': 0.31; 'lists': 0.32; 'something': 0.35; 'equal': 0.35; 'but': 0.35; 'add': 0.35; 'there': 0.35; 'data,': 0.36; 'next': 0.36; 'useful': 0.36; 'subject:?': 0.36; 'should': 0.36; 'two': 0.37; 'list': 0.37; 'to:addr:python-list': 0.38; 'anything': 0.39; "couldn't": 0.39; 'extremely': 0.39; 'to:addr:python.org': 0.39; 'received:org': 0.40; 'up,': 0.60; 'length': 0.61; 'first': 0.61; 'useful.': 0.68; 'obvious': 0.74; 'potentially': 0.81 |
| X-Injected-Via-Gmane | http://gmane.org/ |
| To | python-list@python.org |
| From | Peter Otten <__peter__@web.de> |
| Subject | Re: Splitting a list into even size chunks in python? |
| Date | Wed, 27 Mar 2013 09:27:49 +0100 |
| Organization | None |
| References | <abcd1234abc123ab12a0000000208000020000001052@gmail.com> |
| Mime-Version | 1.0 |
| Content-Type | text/plain; charset="ISO-8859-1" |
| Content-Transfer-Encoding | 7Bit |
| X-Gmane-NNTP-Posting-Host | p5084b921.dip.t-dialin.net |
| User-Agent | KNode/4.7.3 |
| 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.3796.1364372856.2939.python-list@python.org> (permalink) |
| Lines | 28 |
| NNTP-Posting-Host | 2001:888:2000:d::a6 |
| X-Trace | 1364372856 news.xs4all.nl 6931 [2001:888:2000:d::a6]:46197 |
| X-Complaints-To | abuse@xs4all.nl |
| Xref | csiph.com comp.lang.python:41987 |
Show key headers only | View raw
Norah Jones wrote: > I have a list of arbitrary length, and I need to split it up into equal > size chunks. There are some obvious ways to do this, like keeping a > counter and two lists, and when the second list fills up, add it to the > first list and empty the second list for the next round of data, but this > is potentially extremely expensive. > > I was wondering if anyone had a good solution to this for lists of any > length > > This should work: > > l = range(1, 1000) > print chunks(l, 10) -> [ [ 1..10 ], [ 11..20 ], .., [ 991..999 ] ] > > I was looking for something useful in itertools but I couldn't find > anything obviously useful. Look again, for the grouper() recipe. For lists you can also use slicing: >>> items ['a', 'b', 'c', 'd', 'e', 'f', 'g'] >>> n = 3 >>> [items[start:start+n] for start in range(0, len(items), n)] [['a', 'b', 'c'], ['d', 'e', 'f'], ['g']]
Back to comp.lang.python | Previous | Next — Next in thread | Find similar | Unroll thread
Re: Splitting a list into even size chunks in python? Peter Otten <__peter__@web.de> - 2013-03-27 09:27 +0100
Re: Splitting a list into even size chunks in python? Roy Smith <roy@panix.com> - 2013-03-27 08:51 -0400
Re: Splitting a list into even size chunks in python? Chris Angelico <rosuav@gmail.com> - 2013-03-27 23:59 +1100
Re: Splitting a list into even size chunks in python? Roy Smith <roy@panix.com> - 2013-03-27 19:42 -0400
Re: Splitting a list into even size chunks in python? Chris Angelico <rosuav@gmail.com> - 2013-03-28 11:15 +1100
csiph-web