66 lines
1.2 KiB
Python
66 lines
1.2 KiB
Python
|
|
import itertools
|
|
|
|
|
|
def get_state(person):
|
|
return person['state']
|
|
|
|
|
|
people = [
|
|
{
|
|
'name': 'John Doe',
|
|
'city': 'Gotham',
|
|
'state': 'NY'
|
|
},
|
|
{
|
|
'name': 'Jane Doe',
|
|
'city': 'Kings Landing',
|
|
'state': 'NY'
|
|
},
|
|
{
|
|
'name': 'Corey Schafer',
|
|
'city': 'Boulder',
|
|
'state': 'CO'
|
|
},
|
|
{
|
|
'name': 'Al Einstein',
|
|
'city': 'Denver',
|
|
'state': 'CO'
|
|
},
|
|
{
|
|
'name': 'John Henry',
|
|
'city': 'Hinton',
|
|
'state': 'WV'
|
|
},
|
|
{
|
|
'name': 'Randy Moss',
|
|
'city': 'Rand',
|
|
'state': 'WV'
|
|
},
|
|
{
|
|
'name': 'Nicole K',
|
|
'city': 'Asheville',
|
|
'state': 'NC'
|
|
},
|
|
{
|
|
'name': 'Jim Doe',
|
|
'city': 'Charlotte',
|
|
'state': 'NC'
|
|
},
|
|
{
|
|
'name': 'Jane Taylor',
|
|
'city': 'Faketown',
|
|
'state': 'NC'
|
|
}
|
|
]
|
|
|
|
person_group = itertools.groupby(people, get_state)
|
|
|
|
copy1, copy2 = itertools.tee(person_group)
|
|
|
|
for key, group in person_group:
|
|
print(key, len(list(group)))
|
|
# for person in group:
|
|
# print(person)
|
|
# print()
|