在字典列表中添加计数以更正字典

时间:2018-02-19 21:01:23

标签: python python-3.x dictionary python-3.5

我希望对于那些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循环输出与每个'field'的第二个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']) })

3 个答案:

答案 0 :(得分:0)

使用Counter类 https://docs.python.org/3.6/library/collections.html

chromeOptions: { args: ["--headless", "--disable-gpu", "--window-size=1920,1080"] }

它非常快速且易于使用。 虽然,它最适用于iterables(与我在第4行的列表中包装它的方式相反),代码示例将帮助您入门。

(例如,如果您有权访问所有from collections import Counter的列表,则可以location.service_type进行繁忙。完成。

Counter(list)

输出:

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)

答案 1 :(得分: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给出了预期的输出。

答案 2 :(得分:0)

这是一个解决方案:

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]

以下是Counter类的documentation