I'm hoping this is an easy one for those masters of for loops and dictionary lookups...but every attempt has so far yielded duplication of the count.
So, to begin, I have two loops:
for location in locations:
print(location.service_type)
for service_type in service_types:
print(service_type)
First output:
library
railway_station
school
cinema
school
supermarket
fire_station
Second output:
{'field': 'bus_station', 'choice': 'Bus Station', 'count': 0}
{'field': 'car_park', 'choice': 'Car Park', 'count': 0}
{'field': 'cinema', 'choice': 'Cinema', 'count': 0}
{'field': 'dentist', 'choice': 'Dentist', 'count': 0}
{'field': 'doctor', 'choice': 'Doctor', 'count': 0}
{'field': 'fire_station', 'choice': 'Fire Station', 'count': 0}
{'field': 'garden', 'choice': 'Public Garden', 'count': 0}
{'field': 'hospital', 'choice': 'Hospital', 'count': 0}
{'field': 'leisure_centre', 'choice': 'Leisure Centre', 'count': 0}
{'field': 'library', 'choice': 'Library', 'count': 0}
{'field': 'public_service', 'choice': 'Public Service', 'count': 0}
{'field': 'railway_station', 'choice': 'Railway Station', 'count': 0}
{'field': 'school', 'choice': 'School', 'count': 0}
{'field': 'supermarket', 'choice': 'Supermarket', 'count': 0}
{'field': 'woodland', 'choice': 'Woodland', 'count': 0}
What would be the most efficient way to add 1 to the count every time the first for loop's output matches the 'field' value in the second list of dictionaries outputted from the second for loop for each 'field'?
I've started with all manor of loops and lookups but nothing is really getting past understanding how to manipulate the two data structures together coherently. I'm just flat out stuck on this.
Expected output:
list_of_dicts = [
{'field': 'bus_station', 'choice': 'Bus Station', 'count': 0}
{'field': 'car_park', 'choice': 'Car Park', 'count': 0}
{'field': 'cinema', 'choice': 'Cinema', 'count': 1}
{'field': 'dentist', 'choice': 'Dentist', 'count': 0}
{'field': 'doctor', 'choice': 'Doctor', 'count': 0}
{'field': 'fire_station', 'choice': 'Fire Station', 'count': 1}
{'field': 'garden', 'choice': 'Public Garden', 'count': 0}
{'field': 'hospital', 'choice': 'Hospital', 'count': 0}
{'field': 'leisure_centre', 'choice': 'Leisure Centre', 'count': 0}
{'field': 'library', 'choice': 'Library', 'count': 1}
{'field': 'public_service', 'choice': 'Public Service', 'count': 0}
{'field': 'railway_station', 'choice': 'Railway Station', 'count': 1}
{'field': 'school', 'choice': 'School', 'count': 2}
{'field': 'supermarket', 'choice': 'Supermarket', 'count': 1}
{'field': 'woodland', 'choice': 'Woodland', 'count': 0}
]
My best attempt, which kind of works:
service_types = []
_list = []
for location in locations:
_list.append(location.service_type)
for field, choice in fields:
service_types.append({ 'field': field, 'choice': choice, 'count': 0 })
new_service_types = []
for service_type in service_types:
new_service_types.append({ 'field': service_type['field'], 'choice': service_type['choice'], 'count': _list.count(service_type['field']) })
?