我想找到一组数据中的最高温度,并将输出结果打印为“最热的温度是x的y”,其中x和y分别是温度和城市。我有这样的代码:与python输出混淆
data = [['Sheffield', '41.2', '35.5', '41.1'],
['Lancaster', '31.3', '40.2', '37.9'],
['Southampton', '34.8', '33.9', '32',],
['Manchester', '41.9', '41.5', '44.2'],
['Bristol', '42.1', '37.1', '42.2']]
hot = []
for row in data:
for item in row:
if item == max(row[1:]):
hot.append(item)
if max(hot) in row:
print "The hottest temperature was {0} in {1}.".format(max(hot),row[0])
。制得的输出:
The hottest temperature was 41.2 in Sheffield.
The hottest temperature was 44.2 in Manchester.
现在我很困惑与输出。我想只打印一行应该是“曼切斯特最热的温度是44.2”的输出。因为44.2是数据中的最高温度。为什么“谢菲尔德最热的温度是41.2”。也打印?我在哪里弄错了?
谢谢您的解决方案。现在我知道我错了。 – 2013-04-30 13:34:21