Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
iterating a span of a list
#1
i'm looking for something that can do like this span() function can:
a = ['foo','bar','boo','far','baz']
for b in span(a,3):
    print(*b)
a = [1,2,3,4,5,6,7,8,9]
for b in span(a,4):
    print(*b)
Output:
foo bar boo bar boo far boo far baz 1 2 3 4 2 3 4 5 3 4 5 6 4 5 6 7 5 6 7 8 6 7 8 9
this shouldn't be hard to code, but if zip() exists, maybe something like this span() exists. if it would return an iterator, that would be fine.
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
It looks like more_itertools.windowed()
Reply
#3
implementing it was next to trivial. a slice in a comprehension.
def span(s,n):
    return iter([s[x:x+n]for x in range(len(s)-n+1)])
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply
#4
import itertools
from functools import partial


def window(sequence, window_size, uncomplete=True, fillvalue=None):
    if uncomplete:
        izip = partial(itertools.zip_longest, fillvalue=fillvalue)
    else:
        izip = zip
    yield from izip(*[iter(sequence)] * window_size)
l = list(range(10))
for chunk in window(l, 3):
    print(chunk)
Output:
(0, 1, 2) (3, 4, 5) (6, 7, 8) (9, None, None)
If you use uncomplete=False, the zip function will be used.
The zip function stops iterating, if there are not enough values, to fill the window.

l = list(range(10))
for chunk in window(l, 3, uncomplete=False):
    print(chunk)
Output:
(0, 1, 2) (3, 4, 5) (6, 7, 8)
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
Reply
#5
The more_itertools implementation uses a deque, but it works with any iterable, not only iterables with random access.

@DeaD_EyE I think your function does something different.
Reply
#6
yes, DeaD_EyE's result is different.
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
  Delete list while iterating shantanu97 1 1,866 Jun-06-2021, 11:59 AM
Last Post: Yoriz
  Creating a list of dictionaries while iterating pythonnewbie138 6 3,195 Sep-27-2020, 08:23 PM
Last Post: pythonnewbie138
  python3: iterating through list not working wardancer84 3 2,302 Jul-08-2020, 04:30 PM
Last Post: DPaul
  Indexing problem while iterating list and subtracting lbtdne 2 2,087 May-14-2020, 10:19 PM
Last Post: deanhystad
  Here what is the meaning of span=(1,2) ,match='1'? srisrinu 1 2,078 Apr-27-2020, 10:22 AM
Last Post: anbu23
  Iterating through a list of strings Ash_Ren 1 2,061 Nov-22-2019, 08:30 PM
Last Post: buran
  Span columns with docxTemplate python jimmeh 4 4,225 May-29-2019, 04:40 PM
Last Post: heiner55
  iterating a list of ranges Skaperen 1 1,991 May-22-2019, 07:44 AM
Last Post: Gribouillis
  Iterating list of oredereddict for creating new list of ordereddict babypython 7 3,638 May-05-2019, 05:32 PM
Last Post: Larz60+
  Efficient way of iterating a list of records anguis 4 2,985 Feb-19-2019, 03:39 AM
Last Post: scidam

Forum Jump:

User Panel Messages

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