tag:blogger.com,1999:blog-462006525194985726.post5795446613476658318..comments2023-09-22T14:13:08.968-04:00Comments on Sandro Tosi: Python: group a list in sub-lists of n itemsSandro Tosihttp://www.blogger.com/profile/17077191422205823991noreply@blogger.comBlogger13125tag:blogger.com,1999:blog-462006525194985726.post-63751196340544350062020-09-06T06:41:29.763-04:002020-09-06T06:41:29.763-04:00This comment has been removed by a blog administrator.Anonymoushttps://www.blogger.com/profile/16082663046571146000noreply@blogger.comtag:blogger.com,1999:blog-462006525194985726.post-54278294414920149202011-04-21T17:49:57.030-04:002011-04-21T17:49:57.030-04:00@all: thanks for all your replies and alternative ...@all: thanks for all your replies and alternative solutions (some not exactly what I need, but appreciated nonetheless)<br /><br />@Craig: simply because on the server where I need this snippet I only have 2.4 (and upgrade it is not an option)Sandro Tosihttps://www.blogger.com/profile/17077191422205823991noreply@blogger.comtag:blogger.com,1999:blog-462006525194985726.post-42357307458207085112011-04-19T00:30:09.608-04:002011-04-19T00:30:09.608-04:00Nobu
you code doesn't seem to work.
did you a...Nobu<br /><br />you code doesn't seem to work.<br />did you actually test it ?Roberthttps://www.blogger.com/profile/06757914721711308458noreply@blogger.comtag:blogger.com,1999:blog-462006525194985726.post-44736627129952342892011-04-14T07:23:58.920-04:002011-04-14T07:23:58.920-04:00Following your tangent... I'm curious to know ...Following your tangent... I'm curious to know why you're on 2.4. As a small-time package developer, I thought I only had to care about 2.5 and above by now.Anonymousnoreply@blogger.comtag:blogger.com,1999:blog-462006525194985726.post-27998076108121271172011-04-12T11:53:35.231-04:002011-04-12T11:53:35.231-04:00Sticking with iterators...
import itertools
def ...Sticking with iterators...<br /><br />import itertools<br /><br />def group_iter(iterator,n=2):<br /> while True:<br /> li = list(itertools.islice(iterator,n))<br /> if len(li):<br /> yield li<br /> else:<br /> breakJeffhttps://www.blogger.com/profile/16063410591954163374noreply@blogger.comtag:blogger.com,1999:blog-462006525194985726.post-77216069872366641192011-04-11T23:38:52.870-04:002011-04-11T23:38:52.870-04:00Which is why I still consider myself a novice. ;-)...Which is why I still consider myself a novice. ;-)<br /><br />Much more readable than mine. If I could tell what Jaime's was doing, I might've tried something like that....Nobuhttps://www.blogger.com/profile/03479768544623324877noreply@blogger.comtag:blogger.com,1999:blog-462006525194985726.post-37872138594925320632011-04-11T16:42:55.325-04:002011-04-11T16:42:55.325-04:00A simple translation of Jaime's solution to a ...A simple translation of Jaime's solution to a generator function yields times almost identical to Nobu's:<br /><br />def split(sequence, size):<br /> for i in xrange(0, len(sequence), size):<br /> yield sequence[i:i+size]Anonymousnoreply@blogger.comtag:blogger.com,1999:blog-462006525194985726.post-51536944707880463972011-04-11T15:12:51.341-04:002011-04-11T15:12:51.341-04:00>>> seq = [1,2,3,4,5,6,7,8,9,10]
>>...>>> seq = [1,2,3,4,5,6,7,8,9,10]<br />>>> [seq[i::num] for i in range(num)]<br /><br />I wrote a longish blog post about this some time ago... http://www.garyrobinson.net/2008/04/splitting-a-pyt.htmlAnonymousnoreply@blogger.comtag:blogger.com,1999:blog-462006525194985726.post-3895636765455609732011-04-11T13:07:41.798-04:002011-04-11T13:07:41.798-04:00Actually, looks like mine is the fastest of yours,...Actually, looks like mine is the fastest of yours, Jaime's and mine. Of course, this is just a benchmark; it may be different in real-world usage:<br /><br />http://pastebin.ca/2045170<br /><br />Mine is t, yours is t2, Jamie's is t3.<br /><br />Also note, I moved orig = range(23) into the setup part of timeit and that improved the time to 7.75~.Nobuhttps://www.blogger.com/profile/03479768544623324877noreply@blogger.comtag:blogger.com,1999:blog-462006525194985726.post-15410614903271866822011-04-11T12:37:41.252-04:002011-04-11T12:37:41.252-04:00Not as simple or elegant as some of the solutions,...Not as simple or elegant as some of the solutions, and I don't know how efficient it is, but here's something I threw together (substitute spaces for leading dashes; blogger doesn't like <code> tags):<br /><br />def splitarray(array, gsize):<br />--arraylen = len(array)<br />--for i in range(arraylen / gsize):<br />----yield array[i * gsize:(i * gsize) + gsize]<br />--if arraylen % gsize != 0:<br />----yield array[-(arraylen % gsize):]Nobuhttps://www.blogger.com/profile/03479768544623324877noreply@blogger.comtag:blogger.com,1999:blog-462006525194985726.post-88865932923379757132011-04-11T11:42:19.657-04:002011-04-11T11:42:19.657-04:00I'd adapt the grouper recipe. It's based o...I'd adapt the <a href="http://docs.python.org/library/itertools.html#recipes" rel="nofollow">grouper recipe</a>. It's based on izip_longest. I would also upgrade to a recent version of Python -- there are lots of new itertools goodies.Anonymousnoreply@blogger.comtag:blogger.com,1999:blog-462006525194985726.post-26559567637493542462011-04-11T11:27:21.738-04:002011-04-11T11:27:21.738-04:00My favorite solution is zip(*[iter(input)]*N), whe...My favorite solution is zip(*[iter(input)]*N), where input is your input list and N is how many elements you want per sub-list. It's not exactly the same as your solution, since it drops dangling items instead of giving you back a short sublist. Replacing zip() with map(None gives you a solution that None-pads the last sublist if necessary, instead. However, this is "favorite" in a kind of perl golf way, not a use it in real software sort of way.Jean-Paul Calderonehttps://www.blogger.com/profile/15144405957500773467noreply@blogger.comtag:blogger.com,1999:blog-462006525194985726.post-64871333509677473312011-04-11T11:24:11.463-04:002011-04-11T11:24:11.463-04:00What I usually do is this small line...
lists = ...What I usually do is this small line...<br /><br /><br />lists = [original_list[i:i+list_size] for i in xrange(0, len(original_list), list_size)]<br /><br />It's a little scary the first you see it, but it's easy. Just get the indexes from 0 to the length, in steps of list_size. Then create a sublist for each.Jaimehttps://www.blogger.com/profile/02966274109038870865noreply@blogger.com