Posts: 4,647
Threads: 1,494
Joined: Sep 2016
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.
Posts: 1,838
Threads: 2
Joined: Apr 2017
Jul-19-2022, 04:45 AM
(This post was last modified: Jul-19-2022, 04:45 AM by ndc85430.)
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)]
Posts: 8,160
Threads: 160
Joined: Sep 2016
Posts: 1,094
Threads: 143
Joined: Jul 2017
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
Posts: 6,798
Threads: 20
Joined: Feb 2020
Jul-19-2022, 01:28 PM
(This post was last modified: Jul-19-2022, 01:29 PM by deanhystad.)
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)
Posts: 1,950
Threads: 8
Joined: Jun 2018
(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.
Posts: 4,647
Threads: 1,494
Joined: Sep 2016
(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.
Posts: 6,798
Threads: 20
Joined: Feb 2020
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.
Posts: 4,647
Threads: 1,494
Joined: Sep 2016
(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.
|