迭代字符串和浮点数以在字典中找到最小值的问题
Problem of iterating over string and float to find minimum value in dictionary
我需要你的帮助来解决可迭代和不可迭代的值以找到最小值。
输入:
D1={"Arcadia":{"Month": "January","Atmospheric pressure(hPa)":1013.25},"Xanadu":{"Month:"January","Atmospheric Pressure(hPa)":"No information"},"Atlantis":{"Month":"January","Atmospheric Pressure(hPa):2035.89}}
输出:
气压最低的城市是Arcadia,大气压为1013.25
这是我的代码:
def lowest_pressure(dict1):
dict_pressure={}
exclude = "No information"
for city,data in dict1.items():
for value in str(data["Atmospheric Pressure(hPa)"]):
if value != exclude:
dict_pressure[city]=data["Atmospheric Pressure(hPa)"]
low_pressure=min(dict_pressure,key=dict_pressure.get)
print(f"The city that has the lowest Atmospheric Pressure is {low_pressure} with ${dict_pressure[low_pressure}hPa.")
lowest_pressure(D1)
如果有词典理解我也想知道
再次感谢 SO 社区。
将字典内容编辑成我认为应该的样子,生成的代码应该足够了:
D1 = {"Arcadia": {"Month": "January", "Atmospheric Pressure(hPa)": 1013.25},
"Xanadu": {"Month": "January", "Atmospheric Pressure(hPa)": "No information"},
"Atlantis": {"Month": "January", "Atmospheric Pressure(hPa)": 2035.89}
}
t = []
for k, v in D1.items():
try:
t.append((float(v['Atmospheric Pressure(hPa)']), k))
except ValueError:
pass
if t:
p, c = min(t)
print(f'The city with the lowest pressure is {c} with the Atmospheric Pressure of {p:.2f}')
else:
print('No valid pressure measurements available')
输出:
The city with the lowest pressure is Arcadia with the Atmospheric Pressure of 1013.25
D1={"Arcadia":{"Month": "January","Atmospheric Pressure(hPa)":1013.25},"Xanadu":{"Month":"January","Atmospheric Pressure(hPa)":"No information"},"Atlantis":{"Month":"January","Atmospheric Pressure(hPa)":2035.89}}
country_pressure = {a:D1[a]["Atmospheric Pressure(hPa)"] for a in D1}
min_pressure = min([a for a in country_pressure.values() if type(a)==int or type(a) ==float])
lowest_pressure_country = None
for k,v in country_pressure.items():
if v==min_pressure:
lowest_pressure_country = k
break
print(f'The city with lowest pressure is {lowest_pressure_country}')
我需要你的帮助来解决可迭代和不可迭代的值以找到最小值。
输入:
D1={"Arcadia":{"Month": "January","Atmospheric pressure(hPa)":1013.25},"Xanadu":{"Month:"January","Atmospheric Pressure(hPa)":"No information"},"Atlantis":{"Month":"January","Atmospheric Pressure(hPa):2035.89}}
输出:
气压最低的城市是Arcadia,大气压为1013.25
这是我的代码:
def lowest_pressure(dict1):
dict_pressure={}
exclude = "No information"
for city,data in dict1.items():
for value in str(data["Atmospheric Pressure(hPa)"]):
if value != exclude:
dict_pressure[city]=data["Atmospheric Pressure(hPa)"]
low_pressure=min(dict_pressure,key=dict_pressure.get)
print(f"The city that has the lowest Atmospheric Pressure is {low_pressure} with ${dict_pressure[low_pressure}hPa.")
lowest_pressure(D1)
如果有词典理解我也想知道
再次感谢 SO 社区。
将字典内容编辑成我认为应该的样子,生成的代码应该足够了:
D1 = {"Arcadia": {"Month": "January", "Atmospheric Pressure(hPa)": 1013.25},
"Xanadu": {"Month": "January", "Atmospheric Pressure(hPa)": "No information"},
"Atlantis": {"Month": "January", "Atmospheric Pressure(hPa)": 2035.89}
}
t = []
for k, v in D1.items():
try:
t.append((float(v['Atmospheric Pressure(hPa)']), k))
except ValueError:
pass
if t:
p, c = min(t)
print(f'The city with the lowest pressure is {c} with the Atmospheric Pressure of {p:.2f}')
else:
print('No valid pressure measurements available')
输出:
The city with the lowest pressure is Arcadia with the Atmospheric Pressure of 1013.25
D1={"Arcadia":{"Month": "January","Atmospheric Pressure(hPa)":1013.25},"Xanadu":{"Month":"January","Atmospheric Pressure(hPa)":"No information"},"Atlantis":{"Month":"January","Atmospheric Pressure(hPa)":2035.89}}
country_pressure = {a:D1[a]["Atmospheric Pressure(hPa)"] for a in D1}
min_pressure = min([a for a in country_pressure.values() if type(a)==int or type(a) ==float])
lowest_pressure_country = None
for k,v in country_pressure.items():
if v==min_pressure:
lowest_pressure_country = k
break
print(f'The city with lowest pressure is {lowest_pressure_country}')