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

  • Home
  • SEARCH
  • 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 8888487
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T22:03:15+00:00 2026-06-14T22:03:15+00:00

In a project I am currently working on I have implemented about 80% of

  • 0

In a project I am currently working on I have implemented about 80% of what I want my program to do and I am very happy with the results.

In the remaining 20% I am faced with a problem which puzzles me a bit on how to solve.
Here it is:

I have come up with a list of lists which contain several numbers (arbitrary length)
For example:

listElement[0] = [1, 2, 3]
listElement[1] = [3, 6, 8]
listElement[2] = [4, 9]
listElement[4] = [6, 11]
listElement[n] = [x, y, z...]

where n could reach up to 40,000 or so.

Assuming each list element is a set of numbers (in the mathematical sense), what I would like to do is to derive all the combinations of mutually exclusive sets; that is, like the powerset of the above list elements, but with all non-disjoint-set elements excluded.

So, to continue the example with n=4, I would like to come up with a list that has the following combinations:

newlistElement[0] = [1, 2, 3]
newlistElement[1] = [3, 6, 8]
newlistElement[2] = [4, 9]
newlistElement[4] = [6, 11] 
newlistElement[5] = [[1, 2, 3], [4, 9]]
newlistElement[6] = [[1, 2, 3], [6, 11]]
newlistElement[7] = [[1, 2, 3], [4, 9], [6, 11]]
newlistElement[8] = [[3, 6, 8], [4, 9]]
newlistElement[9] = [[4, 9], [6, 11]

An invalid case, for example would be combination [[1, 2, 3], [3, 6, 8]] because 3 is common in two elements.
Is there any elegant way to do this? I would be extremely grateful for any feedback.

I must also specify that I would not like to do the powerset function, because the initial list could have quite a large number of elements (as I said n could go up to 40000), and taking the powerset with so many elements would never finish.

  • 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-14T22:03:16+00:00Added an answer on June 14, 2026 at 10:03 pm

    The method used in the program below is similar to a couple of previous answers in excluding not-disjoint sets and therefore usually not testing all combinations. It differs from previous answers by greedily excluding all the sets it can, as early as it can. This allows it to run several times faster than NPE’s solution. Here is a time comparison of the two methods, using input data with 200, 400, … 1000 size-6 sets having elements in the range 0 to 20:

    Set size =   6,  Number max =  20   NPE method
      0.042s  Sizes: [200, 1534, 67]
      0.281s  Sizes: [400, 6257, 618]
      0.890s  Sizes: [600, 13908, 2043]
      2.097s  Sizes: [800, 24589, 4620]
      4.387s  Sizes: [1000, 39035, 9689]
    
    Set size =   6,  Number max =  20   jwpat7 method
      0.041s  Sizes: [200, 1534, 67]
      0.077s  Sizes: [400, 6257, 618]
      0.167s  Sizes: [600, 13908, 2043]
      0.330s  Sizes: [800, 24589, 4620]
      0.590s  Sizes: [1000, 39035, 9689]
    

    In the above data, the left column shows execution time in seconds. The lists of numbers show how many single, double, or triple unions occurred. Constants in the program specify data set sizes and characteristics.

    #!/usr/bin/python
    from random import sample, seed
    import time
    nsets,   ndelta,  ncount, setsize  = 200, 200, 5, 6
    topnum, ranSeed, shoSets, shoUnion = 20, 1234, 0, 0
    seed(ranSeed)
    print 'Set size = {:3d},  Number max = {:3d}'.format(setsize, topnum)
    
    for casenumber in range(ncount):
        t0 = time.time()
        sets, sizes, ssum = [], [0]*nsets, [0]*(nsets+1);
        for i in range(nsets):
            sets.append(set(sample(xrange(topnum), setsize)))
    
        if shoSets:
            print 'sets = {},  setSize = {},  top# = {},  seed = {}'.format(
                nsets, setsize, topnum, ranSeed)
            print 'Sets:'
            for s in sets: print s
    
        # Method by jwpat7
        def accrue(u, bset, csets):
            for i, c in enumerate(csets):
                y = u + [c]
                yield y
                boc = bset|c
                ts = [s for s in csets[i+1:] if boc.isdisjoint(s)]
                for v in accrue (y, boc, ts):
                    yield v
    
        # Method by NPE
        def comb(input, lst = [], lset = set()):
            if lst:
                yield lst
            for i, el in enumerate(input):
                if lset.isdisjoint(el):
                    for out in comb(input[i+1:], lst + [el], lset | set(el)):
                        yield out
    
        # Uncomment one of the following 2 lines to select method
        #for u in comb (sets):
        for u in accrue ([], set(), sets):
            sizes[len(u)-1] += 1
            if shoUnion: print u
        t1 = time.time()
        for t in range(nsets-1, -1, -1):
            ssum[t] = sizes[t] + ssum[t+1]
        print '{:7.3f}s  Sizes:'.format(t1-t0), [s for (s,t) in zip(sizes, ssum) if t>0]
        nsets += ndelta
    

    Edit: In function accrue, arguments (u, bset, csets) are used as follows:
    • u = list of sets in current union of sets
    • bset = “big set” = flat value of u = elements already used
    • csets = candidate sets = list of sets eligible to be included
    Note that if the first line of accrue is replaced by
    def accrue(csets, u=[], bset=set()):
    and the seventh line by
    for v in accrue (ts, y, boc):
    (ie, if parameters are re-ordered and defaults given for u and bset) then accrue can be invoked via [accrue(listofsets)] to produce its list of compatible unions.

    Regarding the ValueError: zero length field name in format error mentioned in a comment as occurring when using Python 2.6, try the following.

    # change:
        print "Set size = {:3d}, Number max = {:3d}".format(setsize, topnum)
    # to:
        print "Set size = {0:3d}, Number max = {1:3d}".format(setsize, topnum)
    

    Similar changes (adding appropriate field numbers) may be needed in other formats in the program. Note, the what’s new in 2.6 page says “Support for the str.format() method has been backported to Python 2.6”. While it does not say whether field names or numbers are required, it does not show examples without them. By contrast, either way works in 2.7.3.

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

Sidebar

Related Questions

I am currently working on a project about calculations.I have done the main part
I am currently working on android project where I want to have a text
I am currently working on some web dev project in Java, i have implemented
On the project that I'm currently working I have a requirement to run a
I have a MVC 4 project I am currently working on and need some
I am currently working on a project where I have code that looks like
I am currently working on an android project and I have an activity, lets
I am currently working on grails project. I have created eight different plugins. Each
Im currently working on a grizzly, spring and jersey project and i have encountered:
I'm new to objective C and currently working on a small project. I have

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.