filtering names by multiple conditions ( age range and gender) in python 2 -
this in text , code far:
{ "people": [ {"name": "jack", "gender": "m", "age": 25}, {"name": "jerald", "gender": "m", "age": 24}, {"name": "jim", "gender": "m", "age": 23}, {"name": "bill", "gender": "m, "age": 22}, {"name": "barry", "gender": "m", "age": 25}, {"name": "rikki", "gender": "m", "age": 35}, {"name": "ross", "gender": "m", "age": 33}, {"name": "jane", "gender": "f", "age": 25}, {"name": "bela", "gender": "f", "age": 24}, {"name": "cheeky", "gender": "f", "age": 22} ] } import pprint import json my_data = json.loads(open("people.json.txt").read()) print "choose lowest age limit:", age_from = int(raw_input()) print "choose highest age limit:", age_to = int(raw_input()) print "people age %r %r." % ( age_from, age_to) def filter_by_age_range(members, age_from, age_to): print'%s' % name [1]
i trying follow webpage filtering cant seem put in use filter method when 2 conditions met. example if need males (m) , ages 22 25. trying figure out if 1 condition met have far in code programs runs without error doesn't give desired output.i have no idea name[1] does. i'd grateful if me solve problem me understand filter function. btw correct used int() prompting? thanks
your use list comprehension , apply on each row filter function
def filter_by_age( person, age_from, age_to ): if age_from <= person['age'] <= age_to , person['gender']=='m': return true else: false filtered_people = [ p p in my_data['people'] if filter_by_age(p, age_from, age_to) ]
Comments
Post a Comment