这个问题已经在这里有一个答案:
我有一本字典,其中的关键是国家代码,并将该值是一本字典。对于每一个国家,关键就是七种机场类型中的一种,和值是机场名称的列表。
我想,以确定哪些国家具有某种类型的大多数机场。换句话说,哪个国家拥有最多的条目在它的子词典的水上飞机基地。
我怎样才能做到这一点?
正如评论说,一个例子将缓解这一进程。但如果我把你的权利,你有一个dictionary-关键是一个国家(串)的名称和值是另一个字典,其中关键的是那种(串)和机场的值是一个列表总结从相同种类在选定的全国所有机场的名字。你所寻找的是与那种水上飞机基地大部分机场国内:
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!)
你可以使用它返回的长度,例如len()
方法,字典或像这样的列表:
len(dict)