Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6347353
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T21:11:49+00:00 2026-05-24T21:11:49+00:00

This is more a question of elegance and performance rather than how to do

  • 0

This is more a question of elegance and performance rather than “how to do at all”, so I’ll just show the code:

def iterate_adjacencies(gen, fill=0, size=2, do_fill_left=True,
  do_fill_right=False):
    """ Iterates over a 'window' of `size` adjacent elements in the supploed
    `gen` generator, using `fill` to fill edge if `do_fill_left` is True
    (default), and fill the right edge (i.e.  last element and `size-1` of
    `fill` elements as the last item) if `do_fill_right` is True.  """
    fill_size = size - 1
    prev = [fill] * fill_size
    i = 1
    for item in gen:  # iterate over the supplied `whatever`.
        if not do_fill_left and i < size:
            i += 1
        else:
            yield prev + [item]
        prev = prev[1:] + [item]
    if do_fill_right:
        for i in range(fill_size):
            yield prev + [fill]
            prev = prev[1:] + [fill]

and then ask: is there already a function for that? And, if not, can you do the same thing in a better (i.e. more neat and/or more fast) way?

Edit:

with ideas from answers of @agf, @FogleBird, @senderle, a resulting somewhat-neat-looking piece of code is:

def window(seq, size=2, fill=0, fill_left=True, fill_right=False):
    """ Returns a sliding window (of width n) over data from the iterable:
      s -> (s0,s1,...s[n-1]), (s1,s2,...,sn), ...
    """
    ssize = size - 1
    it = chain(
      repeat(fill, ssize * fill_left),
      iter(seq),
      repeat(fill, ssize * fill_right))
    result = tuple(islice(it, size))
    if len(result) == size:  # `<=` if okay to return seq if len(seq) < size
        yield result
    for elem in it:
        result = result[1:] + (elem,)
        yield result
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-24T21:11:50+00:00Added an answer on May 24, 2026 at 9:11 pm

    Resulting function (from the edit of the question),

    frankeniter with ideas from answers of @agf, @FogleBird, @senderle, a resulting somewhat-neat-looking piece of code is:

    from itertools import chain, repeat, islice
    
    def window(seq, size=2, fill=0, fill_left=True, fill_right=False):
        """ Returns a sliding window (of width n) over data from the iterable:
          s -> (s0,s1,...s[n-1]), (s1,s2,...,sn), ...
        """
        ssize = size - 1
        it = chain(
          repeat(fill, ssize * fill_left),
          iter(seq),
          repeat(fill, ssize * fill_right))
        result = tuple(islice(it, size))
        if len(result) == size:  # `<=` if okay to return seq if len(seq) < size
            yield result
        for elem in it:
            result = result[1:] + (elem,)
            yield result
    

    and, for some performance information regarding deque/tuple:

    In [32]: kwa = dict(gen=xrange(1000), size=4, fill=-1, fill_left=True, fill_right=True)
    In [33]: %timeit -n 10000 [a+b+c+d for a,b,c,d in tmpf5.ia(**kwa)]
    10000 loops, best of 3: 358 us per loop
    In [34]: %timeit -n 10000 [a+b+c+d for a,b,c,d in tmpf5.window(**kwa)]
    10000 loops, best of 3: 368 us per loop
    In [36]: %timeit -n 10000 [sum(x) for x in tmpf5.ia(**kwa)]
    10000 loops, best of 3: 340 us per loop
    In [37]: %timeit -n 10000 [sum(x) for x in tmpf5.window(**kwa)]
    10000 loops, best of 3: 432 us per loop
    

    but anyway, if it’s numbers then numpy is likely preferable.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I think this is more like Math question rather than programming but since I'm
This is probally more of an elegance question than functionality. I am looking for
This more of a style question, rather than a how to. So I've got
I'm not talking about making portable code. This is more a question of distribution.
EDIT: This question is more about language engineering than C++ itself. I used C++
This is more of a philosophical question than anything, so give me your thoughts.
(This is more of a curiousity question than any pending disaster :D ) So
This question more falls into the category of best practices, and clean/safe code for
This is more a theoretical question than a practical one, but given I undestand
This is more a curiosity question than a pressing one. This question is looking

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.