I have a list of dictionaries:
people = [{"name": "Roger", "city": "NY", "age": 20, "sex": "M"},
{"name": "Dan", "city": "Boston", "age": 20, "sex": "M"},
{"name": "Roger", "city": "Boston", "age": 21, "sex": "M"},
{"name": "Dana", "city": "Dallas", "age": 30, "sex": "F"}]
I want to catalogue them, for example I choose these keys:
field = ("sex", "age")
I need a function catalogue(field, people)
that give me:
{ "M":
{ 20: [{"name": "Roger", "city": "NY", "age": 20, "sex": "M"},
{"name": "Dan", "city": "Boston", "age": 20, "sex": "M"}],
21: [{"name": "Roger", "city": "Boston", "age": 21, "sex": "M"}]
},
{ "F":
{ 30: [{"name": "Dana", "city": "Dallas", "age": 30, "sex": "F"}] }
}
when len(field)==1
it's simple. I want to do something like this:
c = catalogue(field, people)
for (sex, sex_value) in c.iteritems():
for (age, age_value) in sex_value.iteritems():
print sex, age, age_value["name"]