我想找到一組數據中的最高溫度,並將輸出結果打印爲「最熱的溫度是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