【发布时间】:2019-10-08 18:15:18
【问题描述】:
我正在使用 dict-comprehension 将国家/地区名称作为唯一键返回,并希望值为该国家/地区的城市数量。如何计算元组列表中的城市数量?
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}
【问题讨论】:
-
“我正在使用 dict-comprehension 来返回国家/地区名称”——它在哪里?
标签: python tuples dictionary-comprehension