Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Error in function to return 3 largest values from a list of numbers

I have this data file and I have to find the 3 largest numbers it contains

24.7    25.7    30.6    47.5    62.9    68.5    73.7    67.9    61.1    48.5    39.6    20.0
16.1    19.1    24.2    45.4    61.3    66.5    72.1    68.4    60.2    50.9    37.4    31.1
10.4    21.6    37.4    44.7    53.2    68.0    73.7    68.2    60.7    50.2    37.2    24.6
21.5    14.7    35.0    48.3    54.0    68.2    69.6    65.7    60.8    49.1    33.2    26.0
19.1    20.6    40.2    50.0    55.3    67.7    70.7    70.3    60.6    50.7    35.8    20.7
14.0    24.1    29.4    46.6    58.6    62.2    72.1    71.7    61.9    47.6    34.2    20.4
8.4     19.0    31.4    48.7    61.6    68.1    72.2    70.6    62.5    52.7    36.7    23.8
11.2    20.0    29.6    47.7    55.8    73.2    68.0    67.1    64.9    57.1    37.6    27.7
13.4    17.2    30.8    43.7    62.3    66.4    70.2    71.6    62.1    46.0    32.7    17.3
22.5    25.7    42.3    45.2    55.5    68.9    72.3    72.3    62.5    55.6    38.0    20.4
17.6    20.5    34.2    49.2    54.8    63.8    74.0    67.1    57.7    50.8    36.8    25.5
20.4    19.6    24.6    41.3    61.8    68.5    72.0    71.1    57.3    52.5    40.6    26.2

Therefore I have written the following code, but it only searches the first row of numbers instead of the entire list. Can anyone help to find the error?

def three_highest_temps(f):
    file = open(f, "r")
    largest = 0
    second_largest = 0
    third_largest = 0
    temp = []
    for line in file:
        temps = line.split()

        for i in temps:
            if i > largest:
                largest = i
            elif largest > i > second_largest:
                second_largest = i
            elif second_largest > i > third_largest:
                third_largest = i
        return largest, second_largest, third_largest

print(three_highest_temps("data5.txt"))
like image 964
Sinoda Avatar asked Dec 14 '15 13:12

Sinoda


People also ask

How do you find the max 3 values in Python?

Python 3 - Number max() Method The max() method returns the largest of its arguments i.e. the value closest to positive infinity.

Which function returns the number of items in a list?

In Python, the built-in function len() is used to get the length (the number of items) of a list.

How do you find the highest value in Python?

Python max() Function The max() function returns the item with the highest value, or the item with the highest value in an iterable. If the values are strings, an alphabetically comparison is done.


1 Answers

Your data contains float numbers not integer.

You can use sorted:

>>> data = '''24.7    25.7    30.6    47.5    62.9    68.5    73.7    67.9    61.1    48.5    39.6    20.0
... 16.1    19.1    24.2    45.4    61.3    66.5    72.1    68.4    60.2    50.9    37.4    31.1
... 10.4    21.6    37.4    44.7    53.2    68.0    73.7    68.2    60.7    50.2    37.2    24.6
... 21.5    14.7    35.0    48.3    54.0    68.2    69.6    65.7    60.8    49.1    33.2    26.0
... 19.1    20.6    40.2    50.0    55.3    67.7    70.7    70.3    60.6    50.7    35.8    20.7
... 14.0    24.1    29.4    46.6    58.6    62.2    72.1    71.7    61.9    47.6    34.2    20.4
... 8.4     19.0    31.4    48.7    61.6    68.1    72.2    70.6    62.5    52.7    36.7    23.8
... 11.2    20.0    29.6    47.7    55.8    73.2    68.0    67.1    64.9    57.1    37.6    27.7
... 13.4    17.2    30.8    43.7    62.3    66.4    70.2    71.6    62.1    46.0    32.7    17.3
... 22.5    25.7    42.3    45.2    55.5    68.9    72.3    72.3    62.5    55.6    38.0    20.4
... 17.6    20.5    34.2    49.2    54.8    63.8    74.0    67.1    57.7    50.8    36.8    25.5
... 20.4    19.6    24.6    41.3    61.8    68.5    72.0    71.1    57.3    52.5    40.6    26.2
... '''

>>> sorted(map(float, data.split()), reverse=True)[:3]
[74.0, 73.7, 73.7]

If you want to integer results

>>> temps = sorted(map(float, data.split()), reverse=True)[:3]
>>> map(int, temps)
[74, 73, 73]
like image 136
Adem Öztaş Avatar answered Sep 27 '22 20:09

Adem Öztaş