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 7443297
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T11:18:33+00:00 2026-05-29T11:18:33+00:00

Follows on from this question Algorithm to generate (not quite) spanning set in Python

  • 0

Follows on from this question Algorithm to generate (not quite) spanning set in Python

Given n items in a list, for example n = 7, so this input: [1,2,3,4,5,6,7]

I’d like to generate this set of sets in python:

[1] [2] [3] [4] [5] [6] [7]
[1] [2] [3] [4] [5] [6, 7]
[1] [2] [3] [4] [5, 6, 7]
...
...
[1, 2, 3, 4, 5] [6, 7]

But not:

[1, 2, 3, 4, 5, 6] [7]
or
[1, 2, 3, 4, 5, 6, 7]

From the previous question I have this great answer:

def span(lst):
  yield [lst]
  for i in range(1, len(lst)):
    for x in span(lst[i:]):
      yield [lst[:i]] + x

Is it possible to work within this existing code to produce this more specific output

Thanks

  • 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-29T11:18:33+00:00Added an answer on May 29, 2026 at 11:18 am

    While you could of course just filter the result of the original function using a comprehension like (s for s in span(lst) if max(len(g) for g in s) <= 5), here I present you a recursive solution that doesn’t create the invalid results in the first place:

    def span(lst, most=float("inf")):
      if not lst:
        yield []
        return
    
      for i in range(1, min(len(lst), most) + 1):
        for x in span(lst[i:], most):
          yield [lst[:i]] + x
    
    lst = [1,2,3,4,5,6,7]
    n = 5
    spannings = list(span(lst, n))         
    print '\n'.join(map(str, spannings))
    
    # proof that it produces the correct result
    assert sorted(spannings) == sorted(s for s in span_orig(lst) if max(map(len, s)) <= n)
    # proof that it produces the same result as the old
    # function if called without a second argument
    assert sorted(span_orig(lst)) == sorted(span(lst))
    

    The logic is very similar, although we can’t make use of the yield [lst] “trick” to avoid having to explicitly state the terminating condition len(lst) == 0. I actually think this is cleaner, overall 🙂

    Note how this is a generalization of the original function: If you don’t give it a second argument, it will work the same way the old function did.

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

Sidebar

Related Questions

This follows on from this question: Algorithm to generate spanning set Given this input:
This question follows on from the answer given by Michael Pilat in Preventing Plus
This question follows on from MYSQL join results set wiped results during IN ()
This follows on from this question where I was getting a few answers assuming
This question follows on from this vim search question I have a setting in
This question follows on from a previous question, that has raised a further issue.
this kind of follows on from another question of mine. Basically, once I have
This problem follows on from a previous question . When I run the following
OK, this kind of follows on from my previous question . What I would
This question follows on from my other question on why my app isn't being

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.