Python Forum
is there an itertor of chunks?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
is there an itertor of chunks?
#1
i have a list (or tuple) of many items. here is a very simple case:
foo = (9,8,7,6,5,4,3,2,1,0)
what i want to do is step through the list with a step of 1 and a chunk of 3:
foo = (9,8,7,6,5,4,3,2,1,0)
print('welcome to foo')
for x in stepchunk(foo,1,3):
    print(' '.join(str(y)for y in x))
print('end of foo')
Output:
welcome to foo 9 8 7 8 7 6 7 6 5 6 5 4 5 4 3 4 3 2 3 2 1 2 1 0 end of foo
this can be done with a more complex expression that does its own indexing and slicing (BTDT). i'd like to know if there is some simpler form in the existing library, before i go to write my own. also, what should i name this function if not "stepchunk"? it would be a generator.
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply
#2
The third party library Toolz has a function called sliding_window: https://toolz.readthedocs.io/en/latest/a...ing_window to do this:

>>> import toolz
>>> list(toolz.itertoolz.sliding_window(3, foo))
[(9, 8, 7), (8, 7, 6), (7, 6, 5), (6, 5, 4), (5, 4, 3), (4, 3, 2), (3, 2, 1), (2, 1, 0)]
Reply
#3
more_iterools.windowed
Gribouillis, Skaperen, Larz60+ like this post
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#4
Simple one-size

foo = [9,8,7,6,5,4,3,2,1,0]
for i in range(2, len(foo)):
    print(foo[i-2], foo[i-1], foo[i])
or variable size

chunk_size = input('How big do you like your chunks, Hunk? ')
size = int(chunk_size)
   
indexes = [j for j in range(size, 0, -1)]

for i in range(size, len(foo) + 1):
    mylist = []
    for j in indexes:
        num = i-j
        mylist.append(foo[num])
    print(mylist)
BashBedlam likes this post
Reply
#5
def stepchunk(items, step, chunk_size):
    """Generate overlapping slices of items using a loop."""
    for i in range(0, len(items)-chunk_size+1, step):
        yield(items[i:i+chunk_size])

def stepchunk2(items, step, chunk_size):
    """Return a generator that generates overlapping slices of items using zip."""
    return zip(*[items[i*step:] for i in range(chunk_size)])

for x in stepchunk((9,8,7,6,5,4,3,2,1,0), 1, 3):
    print(x)

for x in stepchunk2((9,8,7,6,5,4,3,2,1,0), 1, 3):
    print(x)
Reply
#6
(Jul-19-2022, 04:11 AM)Skaperen Wrote: i'd like to know if there is some simpler form in the existing library,

Built-in itertools provide recipe grouper (Collect data into non-overlapping fixed-length chunks or blocks).
Gribouillis likes this post
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy

Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Reply
#7
(Jul-20-2022, 05:49 AM)perfringo Wrote: Built-in itertools provide recipe grouper (Collect data into non-overlapping fixed-length chunks or blocks).
the closest thing i see in there is pairwise() which only does chunks of 2 (as far as i can see). what if i want chunks of size chunk_len?
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply
#8
None of the itertools do what you want, but at the bottom of the page there are recipes that show how to use itertools to make other tools. grouper does not do what you want, but sliding_window does.
Reply
#9
(Jul-22-2022, 04:06 PM)deanhystad Wrote: None of the itertools do what you want, but at the bottom of the page there are recipes that show how to use itertools to make other tools. grouper does not do what you want, but sliding_window does.
for a step interval of 1 it looks like it can do that. but what if i want a step interval of N? oh wait, selecting every Nth item of the returned list would be that for a step interval of N. let's see, now, how to slice an interval of N.
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
Sad How to split a String from Text Input into 40 char chunks? lastyle 7 1,158 Aug-01-2023, 09:36 AM
Last Post: Pedroski55
  How to scan huge files and make it in chunks ampai 2 2,609 May-28-2020, 08:20 PM
Last Post: micseydel
  how to divide one big equation entered as a string to small chunks gungurbuz 1 1,627 May-28-2020, 03:46 PM
Last Post: Larz60+
  Calling chunks of code The_Taco_King3 3 3,965 Jan-26-2017, 02:54 PM
Last Post: iFunKtion

Forum Jump:

User Panel Messages

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