1

How to search in a list of objects attribute ? Suppose i want the fname of all object and also the age > 30 or some match found

#!usr/bin/python
import sys
import pickle

class People:
    def __init__(self, fname=None, lname=None, age=None, gender=None):
        self.fname = fname
        self.lname = lname
        self.age = age
        self.gender = gender

    def display(self):

        #fp = open("abc","w")
        #fp.write("F.Name: "+ppl.fname+ "\nLName: "+ ppl.lname + "\nAge: "+     str(ppl.age)+ "\nGender: "+ ppl.gender) 




ppl = [People("Bean", "Sparrow", 22, "M"), People("Adam", "Sandler", 32, "M"),     People("Jack", "Marro", 28, "M")]

fp = open("abc","w")
for person in ppl:
    fp.write("F.Name: "+person.fname+ "\nLName: "+ person.lname+ "\nAge: "+     str(person.age)+ "\nGender: "+ person.gender+"\n\n")
fp.close()
1
  • Reading the answer to this question makes me once again wonder why people default to list comprehensions instead of generators. After all, the latter is more efficient, and you can easily create a list out of them should you need it. Commented Oct 18, 2013 at 7:29

3 Answers 3

2

In Python there's a thing called list comprehensions. And list comprehensions are your friend.

Get all ppl.fname:

all_fnames = [person.fname for person in ppl]

Get all persons with ppl.age greater than 30:

all_greater_than_thirty = [person for person in ppl if person.age > 30]

And so on.

EDIT Since a list comprehension returns a list, you would just use that in place of whatever list you originally had, for example to write to file:

with open("abc", "w") as fp:
    for person in [p for p in ppl if p.age > 30]:
        fp.write(...) # Fill it in with whatever you want to write

Or even better, but more advanced, you would create a method for your People class that would return a string formatted to write to file, something like People.to_string(), then you could do this:

with open("abc", "w") as fp:
    fp.writelines("%s\n" % person.to_string() for person in ppl if person.age > 30)

The advantage is efficiency. Plus it looks nicer.

Sign up to request clarification or add additional context in comments.

4 Comments

how should i write that ? Can i write that in display function ?? I am an newbie
Depends on what you want to do with the results. It seems like you want to write them out to a file?
Suppose i want to access the third object data Jack Marro ..... How would i do that ?
Accessing Python lists is sort of like accessing an array in other languages: ppl[2] (third element from index 0, so it's really index 2).
0

You can get the names of people older than 30 using list comprehension:

names_list = [person.name for person in ppl if person.age > 30]

and do whatever you want to do with them :)

Comments

0

Is this what you're looking for:

for person in [p for p in ppl if (p.fname is "name" and p.age < 30)]:
    # Do whatever you want with it...

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.