我有一个字典,其中的键是国家/地区代码,而值是字典。对于每个国家/地区,键是七个机场类型之一,并且值是机场名称列表。
我正在尝试确定哪个国家的某种类型的机场最多。换句话说,哪个国家的水上飞机基地所属分类中的条目最多。
我该如何完成?
答案 0 :(得分:0)
您可以使用len()
方法,该方法返回例如字典或列表这样的长度:
len(dict)
答案 1 :(得分:0)
如评论中所述,使用示例可以简化此过程。但是如果我说对了,您有一个字典-键是一个国家(字符串)的名称,值是另一个词典,其中键是机场(字符串)的种类,值是一个列表,总结所选国家/地区中所有同类机场的名称。您要寻找的是从水上飞机基地出发的机场最多的国家:
maxAirports = 0 #A variable that would flag what are the most airports from the sea plane kinds (initiated to 0)
maxCountry = "" #A variable that would specify the name of the country that was the last one to change the value of maxAirports (initiated to "")
first_dictionary = {your keys and values...}
for country, airports in first_dictionary.items():
for airport_type, airport_names in airports.items():
if airport_type == seaPlane_Base:
if maxAirports < len(airport_names):
maxAirports = len(airport_names)
maxCountry = country
print ("The country with the most Sea Plane Bases is " + maxCountry + " and it has " + maxAirports " airports in total!)