给出了名称及其在地球上的权重的字典。找出它们在月球上的重量。 (使用地图和lambda函数)体重(kg)WeightOnEarth = {'John':45,'Shelly':65,'Marry':35}月球GMoon上的重力= 1.622地球上的重力garth =词典给出名字及其在地球上的权重。找出它们在月球上的重量。 (使用地图和lambda函数)体重(kg)WeightOnEarth = {'John':45,'Shelly':65,'Marry':35}月球GMoon上的重力= 1.622地球上的重力= 9.81 < / p>
输出:月球重量:{'约翰':7.44,'雪莱':10.75,'结婚':5.79}
答案 0 :(得分:0)
这里
d = {'John':45, 'Shelly':65, 'Marry':35}
calc = lambda k: (k, (d[k] / 9.81) * 1.622 )
r = dict(calc(k) for k in d)
print(r)
您可以进一步使其漂亮并找到舍入值
d = {'John':45, 'Shelly':65, 'Marry':35}
calc_grav = lambda x: round((x / 9.81) * 1.622, 2)
calc = lambda k: (k, calc_grav(d[k]))
r = dict(calc(k) for k in d)
print(r)
答案 1 :(得分:0)
d = {'John':45, 'Shelly':65, 'Marry':35}
calc_grav = lambda x: round((x / 9.81) * 1.622, 2)
calc = lambda k: (k, calc_grav(d[k]))
r = dict(calc(k) for k in d)
print(r)
答案 2 :(得分:0)
WeightOnEarth = {'John':45, 'Shelly':65, 'Marry':35}
GMoon = 1.622
GEarth = 9.81
dict(map(lambda x : (x, round((WeightOnEarth[x]/GEarth)*GMoon, 2)), WeightOnEarth))