如何在对象属性列表中搜索?假设我想要所有对象的 fname 以及年龄 > 30 或找到一些匹配项
#!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()