0

I want to find the maximum temperature in a set of data and print the output as "The hottest temperature was x in y" where x and y is temperature and city respectively. I have a code like this:

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 outputs that were produced:

The hottest temperature was 41.2 in Sheffield.
The hottest temperature was 44.2 in Manchester.

Now I am confused with the outputs. I want to print only one line of output which is supposed to be "The hottest temperature was 44.2 in Manchester." since 44.2 is the maximum temperature in the data. Why did "The hottest temperature was 41.2 in Sheffield." is printed too? Where did I get it wrong?

5 Answers 5

1

You check if the maximum value of hot is in row for each row, rather than checking once after all the rows have been processed.

Try this:

hot = []
for row in data:
    for item in row:
        if item == max(row[1:]):
           hot.append(item)

    if max(hot) in row:
       max_row = row

print "The hottest temperature was {0} in {1}.".format(max(hot),max_row[0])   

As an aside, you are storing all your temperatures as strings, not floats. You could get an odd result if there is a much wider spread of temperatures ('5' > '35.3' is true, for example).

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you for your solution. Now I know where I got it wrong.
1

You're building up the list as you iterate, and the max is operating on the list as you have it so far. When you get to Sheffield, it's the hottest you've seen so far, so it prints. But it can't know that Manchester is even hotter, as it hasn't seen it yet.

The quickest way to fix this would be to do two loops: one to build up the list, then a second to find the hottest.

(And, 44.2 in Manchester? In your dreams.)

Comments

1
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) 

for row in data:
    if max(hot) in row:
         print "The hottest temperature was {0} in {1}.".format(max(hot),row[0])

Try the above one this should work as you expected...

2 Comments

Code inside answers on Stack Overflow should be explained, not just pasted as is, in order to make the user who asked the question understand your answer.
@whatyouhide : Thanks for your suggestion, i started posting the answers quite recently from now onwards i strictly follow the your suggestion.
0

First of all I wanna say this is not efficient way to do what you want. But if you wanna know why you are getting this result, I'll explain it for you;

  1. You are creating hot list for every list element of data and finding max value in hot list. In first loop it is 41.2 and it is really inside of the first row. So, it prints it normally.
  2. Until loop is for third list element of data , there is no max value than 41.2 and there is no printing.
  3. When the loops is for third list element in data, the max value is 44.2 and it is printed and there will be no max value than this after now and there will be no printing.

Comments

0

A two line, quite "pythonic" way of doing it:

hot = sorted([(max(x[1:]), x[0]) for x in data], key=lambda x: x[0])[-1]
print "The hottest temperature was {0} in {1}.".format(*hot)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.