将计数添加到字典列表中的正确字典

Adding count to correct dictionary in list of dictionaries

我希望这对于那些精通 for 循环和字典查找的人来说是一个简单的...但是到目前为止,每次尝试都产生了重复的计数。

所以,首先,我有两个循环:

for location in locations:
    print(location.service_type)

for service_type in service_types:
    print(service_type)

第一个输出:

library
railway_station
school
cinema
school
supermarket
fire_station

第二个输出:

{'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}

每当第一个 for 循环的输出与每个 [=33 的第二个 for 循环输出的第二个字典列表中的 'field' 值匹配时,将计数加 1 的最有效方法是什么? =]?

我已经从所有循环和查找开始,但没有什么能真正理解如何连贯地一起操作这两个数据结构。我完全坚持这一点。

预期输出:

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}
]

我最好的尝试,哪种作品:

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']) })

?

使用计数器class https://docs.python.org/3.6/library/collections.html

from collections import Counter

速度非常快,而且非常容易使用。 尽管它最适合迭代器(与我在此处第 4 行将其包装在列表中的方式相反),但代码示例将帮助您入门。

(例如,如果您可以访问所有 location.service_type 的列表,您可以只执行 Counter(list) 和 boom。完成。

counter = Counter()
list_of_dicts = []
for location in locations:
    counter.update([location.service_type])
print(counter)

for service_type in service_types:
    if service_type in counter:
        service_types['count'] = counter[service_type]
#and because you have specific output reqs
for service_type in service_types:
    list_of_dicts.append(service_type)

输出:

{
   'cinema': 1,
   'fire_station': 1,
   'library': 1,
   'railway_station': 1,
   'school': 2,
   'supermarket': 1
}


[
   {'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}
]

我建议你这个简单的循环:

for service_type in service_types:
    field = service_type['field']
    if field in locations:
        service_type['count'] += locations.count(field)

print(service_types)

列表 service_types 给出了预期的输出。

这是一个解决方案:

from collections import Counter

# 1. count the occurrence of locations
cnt = Counter(lcn.service_type for lcn in locations)


# 2a. if you don't want a duplicate you can just do this
for st in service_types:
    st['count'] += cnt[st['field']]

# 2b. OR, if you do want a duplicate into a new list
new_list = [dict(st, count=(st['count'] + cnt[st['field']])) 
                for st in service_types]

这是柜台 class 上的 documentation