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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T04:15:42+00:00 2026-06-03T04:15:42+00:00

I am copying an example from python docs . def powerset(iterable): powerset([1,2,3]) –> ()

  • 0

I am copying an example from python docs.

def powerset(iterable):
    "powerset([1,2,3]) --> () (1,) (2,) (3,) (1,2) (1,3) (2,3) (1,2,3)"
    s = list(iterable)
    return chain.from_iterable(combinations(s, r) for r in range(len(s)+1))

How could we randomize the order of the values that we get while the result of powerset remains lazily evaluated?

EDIT: The reason I want it is that I would like to compute the sum of the derived sets and stop as soon as I find two sets that have the same sum. If I am not mistaken, the problem is NP-complete.

  • 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-06-03T04:15:43+00:00Added an answer on June 3, 2026 at 4:15 am

    itertools.combinations() gives us results in a set order from the input. Given this, we can shuffle our input list to produce a random order of elements (obviously, there will be a lot less possible orders for the outcome).

    def random_powerset(iterable):
         s = list(iterable)
         lengths = list(range(len(s)+1))
         shuffle(lengths)
         return chain.from_iterable(combinations(s, r) for r in lengths if not shuffle(s))
    

    (This is a bit of an ugly hack – we know shuffle(s) will always return False, so we can add it as a condition to ensure it’s run for each call of combinations().)

    We pre-generate the list of lengths, so that we can shuffle that too.

    It’s not perfectly random (there will still be an order – all elements of length n will be clustered together, for example, and those elements will be in an order depending on the random order of the input), but there will be a fair amount of randomness, if that is enough for you.

    Example output:

    >>> list(random_powerset(range(3)))
    [(), (2,), (0,), (1,), (2, 1), (2, 0), (1, 0), (1, 2, 0)]
    >>> list(random_powerset(range(3)))
    [(), (0, 1), (0, 2), (1, 2), (0, 1, 2), (2,), (0,), (1,)]
    >>> list(random_powerset(range(3)))
    [(0, 1, 2), (2,), (1,), (0,), (0, 2), (0, 1), (2, 1), ()]
    >>> list(random_powerset(range(3)))
    [(1, 2, 0), (0,), (2,), (1,), (), (0, 1), (0, 2), (1, 2)]
    >>> list(random_powerset(range(3)))
    [(), (2, 1), (2, 0), (1, 0), (0,), (2,), (1,), (2, 1, 0)]
    >>> list(random_powerset(range(3)))
    [(1, 0), (1, 2), (0, 2), (0, 2, 1), (), (1,), (0,), (2,)]
    

    I think that’s the best you can do without making it non-lazy.

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

Sidebar

Related Questions

Copying a File using a straight-forward approach in Python is typically like this: def
Copying directory from source tree to binary tree. For example: How to copy www
I am copying an example from XSLT Cookbook: 2nd Edition (O'Reilly: Mangano, 2006) where
Looking at the example of form_for from the rails docs , I see this
From the MSDN forums , even copying and pasting the simplest example from the
I want to convert text which user is copying from my textbox. For example:
Just wondering about the performance impact of copying very large php variables. For example
I am copying code from website matplotlib and pasting into the Vim editor in
I'm copying ListViewItems from one ListView to another, sth. like: foreach (ListViewItem item in
I'm having trouble copying a row from one table to another using sqlite3 in

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.