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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T03:53:07+00:00 2026-06-04T03:53:07+00:00

How can I create a sublist of class instances satisfying some condition on their

  • 0

How can I create a sublist of class instances satisfying some condition on their attribute values, starting from the complete list of class instances?

For example, I have a list of instances of my class Person(). These persons have many attributes, among which ID, a unique identifier, and HH_ID, the identifier of the household they live in.
I want to connect all the persons living in the same household, and therefore having the same HH_ID. By “connect”, I mean creating an edge between all the household members, using networkx.
In order to do this, I need to identify these persons and put them in a “sublist”, in order to be processed by some algorithm to connect them all together. How can I achieve this?

I need also a general tool to do this for other more complex purposes (e.g. randomly connect N persons aged from 15 to 20 year), but in the easiest case of household members, I can use the fact that my list is ordered by ID and HH_ID, therefore I have something like:

ID HH_ID
0  0
1  0
2  0
3  1
4  1
5  2

where the first household is composed of persons [0,1,2], the second of persons [3,4] and so on…

For this household problem I have tried using the pairwise iterator recipe (itertools documentation in this way:

import pairwise
i = pairwise(personList)
for p in personList:
    toConnectList = [p]
    p1,p2 = i.next()
    while p1.hh_id == p2_hh_id
        toConnectList.append(p2)
        p1,p2 = i.next()
        # connect all persons in toConnectList

But obviously this doesn’t work, as my iterator i goes down until hh_id of the two adjacent persons don’t match, and restarts from there for the next person. E.g. for the persons in the above example, my iterator will start to compare persons 2 and 3 when it comes to person 1 in the for loop, while I would need some way to jump directly to person 3 in the for loop and have my iterator start comparing person 3 and 4.
I hope this example clarifies a bit, even if it doesn’t look very clear…

More generally, I need a way of creating a sublist of persons satisfying some conditions on their attribute values, possibly an efficient way (I have around 150000 persons)

  • 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-04T03:53:09+00:00Added an answer on June 4, 2026 at 3:53 am
    from itertools import groupby
    
    def family_key(person):
        return person.HH_ID
    
    persons.sort(key=family_key)
    for hh_id, family in groupby(persons, key=family_key):
        for person in family:
            # do your thing
    

    For more complex purposes, just alter the key function to return the same value for those items you want to group.

    EDIT: Fixed error: groupby returns a tuple (key, group_iter), not just group_iter.

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

Sidebar

Related Questions

Can I create instance of abstract class in C#/.net like in Java ? Additional
I am retrieving some values from DB in form of ArrayList of 240 size
If I have List<String> text how can I create a sub-list of all continious
I can create a stack class quite easily, using push and pop accessor methods
I can create a page from a file with: Page page = BuildManager.CreateInstanceFromVirtualPath( virtualPath,
You can create a class reference with the following code: Class M = [NSMutableString
I can create an NSArray that contains all the hash values of the objects
I can create a class which extends Thread and overrides the run method with
I can create the database just fine, I can Insert and view the values,
I can create and use dynamic two dimensional array in Fortran (in 77 standard).

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.