[英]Putting a list of integers into an existing dictionary
This is the Bonus part of the "Mountain Heights 3" exercise from http://introtopython.org/dictionaries.html .这是来自http://introtopython.org/dictionaries.html的“高山高地 3”练习的奖励部分。
I have this dictionary which shows 5 mountains and their heights in meters.我有这本字典,上面显示了 5 座山脉及其高度(以米为单位)。
mountains_meters = {'Mount Everest' : 8848,
'K2' : 8611,
'Kangchenjunga' : 8586,
'Lhotse' : 8516,
'Makalu' : 8485,
}
and the question asks to define a function that reads through the height in meters and returns a list of the height in feet, given the conversion 1 meter = 3.28 feet.并且问题要求定义一个 function 读取以米为单位的高度并返回以英尺为单位的高度列表,给定转换 1 米 = 3.28 英尺。
feet = []
def meters_to_feet(dictionary):
for value in dictionary.values():
feet.append(round(value * 3.28))
The question then asks to create a nested dictionary with the structure {'everest': [8848, 29021]}然后问题要求创建一个嵌套字典,其结构为 {'everest': [8848, 29021]}
I'm unsure how to get my list of heights in feet into the existing mountains_meters dictionary.我不确定如何将我的英尺高度列表放入现有的 mountain_meters 字典中。
[29021, 28244, 28162, 27932, 27831]
into [29021, 28244, 28162, 27932, 27831]
进入
mountains_meters = {'Mount Everest' : 8848,
'K2' : 8611,
'Kangchenjunga' : 8586,
'Lhotse' : 8516,
'Makalu' : 8485,
}
Why don't you do it in a single loop?你为什么不在一个循环中做呢?
for k,v in mountains_meters.items():
mountains_meters[k] = [v, round(v*3.28)]
feet.append(round(v * 3.28))
Approach - 1
方法 - 1
You can use a tuple
or list
as a value for each respective mountain range您可以使用
tuple
或list
作为每个相应山脉的值
Each index will define the unit of measurement每个索引将定义测量单位
from pprint import pprint
mountains_meters = {'Mount Everest' : 8848,
'K2' : 8611,
'Kangchenjunga' : 8586,
'Lhotse' : 8516,
'Makalu' : 8485,
}
for key in mountains_meters:
meters = mountains_meters[key]
feet = round(meters* 3.28)
mountains_meters[key] = (meters,feet)
>>> pprint(mountains_meters)
{'K2': (8611, 28244),
'Kangchenjunga': (8586, 28162),
'Lhotse': (8516, 27932),
'Makalu': (8485, 27831),
'Mount Everest': (8848, 29021)}
Approach - 2
方法 - 2
You can create a secondary Dictionary
to hold the feet conversion您可以创建一个辅助
Dictionary
来保存脚转换
from pprint import pprint
mountains_meters = {'Mount Everest' : 8848,
'K2' : 8611,
'Kangchenjunga' : 8586,
'Lhotse' : 8516,
'Makalu' : 8485,
}
mountains_feets = {}
for key in mountains_meters:
meters = mountains_meters[key]
feet = round(meters* 3.28)
mountains_feets [key] = feet
>>> pprint(mountains_feets)
{'K2': 28244,
'Kangchenjunga': 28162,
'Lhotse': 27932,
'Makalu': 27831,
'Mount Everest': 29021}
You can do it like that:你可以这样做:
def m_to_feet(m):
return round(3.28 * m)
nested_dict = {}
for mountain, height_m in mountains_meters.items():
nested_dict[mountain] = [height_m, m_to_feet(height_m)]
Or in a short oneliner with a dict comprehension:或者在具有 dict 理解的简短单行器中:
def m_to_feet(m):
return round(3.28 * m)
nested_dict = {k: [v, m_to_feet(v)] for k, v in mountains_meters.items()}
def h_f(mountains):
for key, value in mountains.items():
mountains[key] = [value, round(value*3.28)]
return mountains
mountains_meters = {'Mount Everest': 8848,
'K2': 8611,
'Kangchenjunga': 8586,
'Lhotse': 8516,
'Makalu': 8485,
}
print(h_f(mountains_meters))
Output Output
{'Mount Everest': [8848, 29021], 'K2': [8611, 28244], 'Kangchenjunga': [8586, 28162], 'Lhotse': [8516, 27932], 'Makalu': [8485, 27831]}
for i, k in enumerate(mountains_meters):
mountains_meters[k] = [mountains_meters[k], feet[i]]
>>> mountains_meters
{'Mount Everest': [8848, 29021], 'K2': [8611, 28244], 'Kangchenjunga': [8586, 28162], 'Lhotse': [8516, 27932], 'Makalu': [8485, 27831]}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.