Python Forum
Code golfing: splitting a list
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Code golfing: splitting a list
#4
(Jan-12-2017, 01:26 AM)ichabod801 Wrote:

Assumes N divides evenly into len(L). If that's a concern you could put a second index into the slice double zip it to account for that.

This is more or less what my code does, but with slightly clumsier Python. And as far as I can tell this works even if N is doesn't divide len(N).

(Jan-12-2017, 02:41 AM)Mekire Wrote: So you want to chunk?
Lots of good answers here:
http://stackoverflow.com/questions/31244...zed-chunks

The standard idiom people generally use is:
chunks = (L[i:i + N] for i in range(0, len(L), N))
This assumes the whole sequence fits in memory which it sounds like you are trying to avoid.
No, in the real-life problem I am handling rather small lists, it just that each item ends up used in a webservice call so to speed things up I create N threads and give each a part of the original list.

(Jan-12-2017, 02:41 AM)Mekire Wrote: Alternatives use iter and zip and look like this:
chunks_zip = zip(*[iter(L)]*N)
The second version above ignores groups that are not full.  But can handle generators that wouldn't otherwise fit in memory:
Python3
L = range(10**25)
N = 3

chunks_zip = zip(*[iter(L)]*N)

for i in range(10):
    print(next(chunks_zip))
Output:
(0, 1, 2) (3, 4, 5) (6, 7, 8) (9, 10, 11) (12, 13, 14) (15, 16, 17) (18, 19, 20) (21, 22, 23) (24, 25, 26) (27, 28, 29)
Use zip_longest with a fill value if you don't want to lose incomplete groups.
Clever, but if you are unlucky, one of the chunks produced by the idioms above can have a length of 1 so the maximum size difference is N-1. With the 1 every N sampling you get more uniform sizes (but possibly using more CPU/memory, which isn't really a concern for me but could be for someone else).
Unless noted otherwise, code in my posts should be understood as "coding suggestions", and its use may require more neurones than the two necessary for Ctrl-C/Ctrl-V.
Your one-stop place for all your GIMP needs: gimp-forum.net
Reply


Messages In This Thread
Code golfing: splitting a list - by Ofnuts - Jan-11-2017, 10:04 PM
RE: Code golfing: splitting a list - by ichabod801 - Jan-12-2017, 01:26 AM
RE: Code golfing: splitting a list - by Ofnuts - Jan-12-2017, 08:10 AM
RE: Code golfing: splitting a list - by Mekire - Jan-12-2017, 02:41 AM
RE: Code golfing: splitting a list - by wavic - Jan-12-2017, 09:13 AM
RE: Code golfing: splitting a list - by Ofnuts - Jan-12-2017, 11:33 AM
RE: Code golfing: splitting a list - by ichabod801 - Jan-12-2017, 10:50 PM
RE: Code golfing: splitting a list - by wavic - Jan-12-2017, 11:01 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  "pythonic" way of splitting up my code? liggisten 4 755 Dec-30-2023, 08:23 PM
Last Post: Gribouillis
  Splitting code into several files TLammert 4 1,533 Jun-26-2022, 02:33 PM
Last Post: TLammert
  Splitting strings in list of strings jesse68 3 1,829 Mar-02-2022, 05:15 PM
Last Post: DeaD_EyE
  Splitting String into 2d list cclark135 2 2,841 Aug-26-2019, 01:46 PM
Last Post: ThomasL
  splitting numeric list based on condition python_newbie09 7 9,530 May-27-2019, 03:58 PM
Last Post: python_newbie09
  Need help | splitting list into list Vinci141 3 2,639 Mar-13-2019, 09:09 PM
Last Post: Vinci141
  some ideas for intelligent list splitting? wardancer84 4 3,261 Nov-20-2018, 02:47 PM
Last Post: DeaD_EyE
  Splitting a list help johiah 5 4,761 Mar-21-2017, 04:31 PM
Last Post: metulburr

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020