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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T06:28:18+00:00 2026-05-14T06:28:18+00:00

This is just an example, but given the following model: class Foo(models.model): bar =

  • 0

This is just an example, but given the following model:

class Foo(models.model):
    bar = models.IntegerField()

    def __str__(self):
        return  str(self.bar)

    def __unicode__(self):
        return str(self.bar)

And the following QuerySet object:

foobar = Foo.objects.filter(bar__lt=20).distinct()

(meaning, a set of unique Foo models with bar <= 20), how can I generate all possible subsets of foobar? Ideally, I’d like to further limit the subsets so that, for each subset x of foobar, the sum of all f.bar in x (where f is a model of type Foo) is between some maximum and minimum value.

So, for example, given the following instance of foobar:

>> print foobar
[<Foo: 5>, <Foo: 10>, <Foo: 15>]

And min=5, max=25, I’d like to build an object (preferably a QuerySet, but possibly a list) that looks like this:

[[<Foo: 5>], [<Foo: 10>], [<Foo: 15>], [<Foo: 5>, <Foo: 10>],
 [<Foo: 5>, <Foo: 15>], [<Foo: 10>, <Foo: 15>]]

I’ve experimented with itertools but it doesn’t seem particularly well-suited to my needs.

I think this could be accomplished with a complex QuerySet but I’m not sure how to start.

  • 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-14T06:28:19+00:00Added an answer on May 14, 2026 at 6:28 am

    This will give you the powerset of foobar (as a list)

    from itertools import combinations
    [j for i in range(len(foobar)+1) for j in combinations(foobar,i)]
    

    Adding a filter for the minval and maxval gives:

    from itertools import combinations
    [j for i in range(len(foobar)+1) for j in combinations(foobar,i)
     if minval <= sum(f.bar for f in j) <= maxval]
    

    Lets create a class and try it out

    >>> from itertools import combinations
    >>> class Foo(object):
    ...     def __init__(self, bar):
    ...         self.bar=bar
    ...     def __repr__(self):
    ...         return  "<Foo: %s>"%self.bar
    ... 
    >>> foobar=[Foo(5),Foo(10),Foo(15)]
    >>> minval=5
    >>> maxval=25
    >>> [j for i in range(len(foobar)+1) for j in combinations(foobar,i) 
         if minval <= sum(f.bar for f in j) <= maxval]
    [(<Foo: 5>,), (<Foo: 10>,), (<Foo: 15>,), (<Foo: 5>, <Foo: 10>), (<Foo: 5>, <Foo: 15>), (<Foo: 10>, <Foo: 15>)]
    

    If you need lists rather than tuples it’s trivial to add that in too

    >>> [list(j) for i in range(len(foobar)+1) for j in combinations(foobar,i) if minval <= sum(f.bar for f in j) <= maxval ]
    [[<Foo: 5>], [<Foo: 10>], [<Foo: 15>], [<Foo: 5>, <Foo: 10>], [<Foo: 5>, <Foo: 15>], [<Foo: 10>, <Foo: 15>]]
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Given the following example: class AnonymousSession << Struct.new(:location, :preferences) def valid? ... end def
For example: > function foo() { > jQuery(whatever).each( function() { return; // this just
Best explained with code I think, this is just a simple example: public class
Given the following example class: public class MyClass { public string S { get;
Given an arbitrary object: class Val(object): def __init__(self): this_val = 123 I want to
Input: $string = this-is-just-an--example Output: this is just an-example Tried various things centered around
i have a recursive query like this (note: this is just an example): var
This is just a simple question. Either way works. I prefer my first example,
Just using this as an example... Here are the columns in my UserProfile table:
I just tossed this super simple code example into a Flash CS4 IDE frame

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.