使用字典理解以 return 国家/地区作为键,以国家/地区的城市总数作为值

Use a dictionary-comprehension to return country as key and a total number of cities for country as value

我正在使用对 return 国家/地区名称的字典理解作为唯一键,并希望值是该国家/地区的城市数量。如何计算元组列表中的城市数量?

country_city_tuples= [('Netherlands', 'Alkmaar'),
                      ('Netherlands', 'Tilburg'),
                      ('Netherlands', 'Den Bosch'),
                      ('Netherlands', 'Eindhoven'),
                      ('Spain', 'Madrid'),
                      ('Spain', 'Barcelona'),
                      ('Spain', 'Cordoba'),
                      ('Spain', 'Toledo'),
                      ('Italy', 'Milano'),
                      ('Italy', 'Roma')]

country_names = { 

}

预期结果为:{'Italy': 2 , 'Netherlands': 4, 'Spain': 4}

使用defaultdict:

from collections import defaultdict

country_names  = defaultdict(int)
for i in country_city_tuples:
    country_names[i[0]]+=1

country_names
defaultdict(int, {'Netherlands': 4, 'Spain': 4, 'Italy': 2})

您可以使用 zip 从元组列表中提取国家名称,然后使用 collections.Counter 计算国家名称的频率

from collections import Counter

country_city_tuples= [('Netherlands', 'Alkmaar'),
                      ('Netherlands', 'Tilburg'),
                      ('Netherlands', 'Den Bosch'),
                      ('Netherlands', 'Eindhoven'),
                      ('Spain', 'Madrid'),
                      ('Spain', 'Barcelona'),
                      ('Spain', 'Cordoba'),
                      ('Spain', 'Toledo'),
                      ('Italy', 'Milano'),
                      ('Italy', 'Roma')]

#Extract out country names using zip and list unpacking
country_names, _ = zip(*country_city_tuples)

#Count the number of countries using Counter
print(dict(Counter(country_names)))

不使用collections,我们可以使用字典来收集频率

country_city_tuples= [('Netherlands', 'Alkmaar'),
                      ('Netherlands', 'Tilburg'),
                      ('Netherlands', 'Den Bosch'),
                      ('Netherlands', 'Eindhoven'),
                      ('Spain', 'Madrid'),
                      ('Spain', 'Barcelona'),
                      ('Spain', 'Cordoba'),
                      ('Spain', 'Toledo'),
                      ('Italy', 'Milano'),
                      ('Italy', 'Roma')]

#Extract out country names using zip and list unpacking
country_names, _ = zip(*country_city_tuples)

result = {}

#Count each country
for name in country_names:
    result.setdefault(name,0)
    result[name] += 1

print(result)

两种情况下的输出相同

{'Netherlands': 4, 'Spain': 4, 'Italy': 2}

试试这个:

l = set(i[0] for i in country_city_tuples)
d = {}
for i in l:
   d[i] = sum([1 for j in country_city_tuples if j[0]==i])

输出 :

{'Italy': 2, 'Netherlands': 4, 'Spain': 4}

使用带有生成器的 sum returns 1 如果国家名称与正在检查的国家匹配,否则为 0:

{name: sum(1 if c[0] == name else 0
           for c in country_city_tuples)
 for name in set(c[0] for c in country_city_tuples)}

您也可以使用 dict.get:

r = {}
for name, city in country_city_tuples:
    r.get(name, 0) += 1

不使用defaultdict和其他模块

country_city_tuples= [('Netherlands', 'Alkmaar'),
                      ('Netherlands', 'Tilburg'),
                      ('Netherlands', 'Den Bosch'),
                      ('Netherlands', 'Eindhoven'),
                      ('Spain', 'Madrid'),
                      ('Spain', 'Barcelona'),
                      ('Spain', 'Cordoba'),
                      ('Spain', 'Toledo'),
                      ('Italy', 'Milano'),
                      ('Italy', 'Roma')]

country_names ={}
for i in country_city_tuples:
    try:
        if country_names[i[0]]:
            country_names[i[0]]+=1
    except:
        country_names[i[0]]=1
print(country_names)

#output {'Netherlands': 4, 'Spain': 4, 'Italy': 2}