Hi. I need some help. I need my python program to get information from the .txt file.(text file). I have managed to get this:

import string
months = []
temperatures = []
fm = open("weather.txt","r")
count = int(fm.readline())
# Number of the moths are read from the .txt file
for indeks in range(count):
    month = fm.readline()
    month = month.strip()
    months += [month]
print(months)
#temperatures are read from the .txt file
for indeks in range(count):
    temperature = fm.readline()
    temperatures_list = temperature.split(" ")
    temperature_i = []
# temperatures are put to list
    for number_s in temperatures_list:
        temperature_i += [int(number_s)]
    temperatures += [temperature_i]
fm.close()
print(temperatures)
print("Data has been received")

#What to do now? :confused:

The text file looks like this:

12
January
February
March
April
May
June
July
August
September
October
November
December
-7 -2 -4 -5 -12 -3 -6 -8 -9 -11 -15 -10 -10 -10 -19 -17 -25 -17 -10 -18 -14 -15 -14 -10 -9 -18 -19 -17 -13 -12 -10
-9 -7 -6 -11 -15 -14 -16 -13 -12 -7 -6 -5 -12 -11 -7 -12 -13 -14 -16 -17 -14 -13 -13 -19 -20 -16 -15 -8
-5 -4 -6 -5 -7 -6 -5 -4 -7 -6 -5 -4 -3 -5 -6 -4 -5 -3 -2 -1 -5 -4 -7 -4 -3 -2 -4 -3 -4 -2
1 2 2 3 4 3 2 4 5 6 7 5 4 5 6 7 4 8 7 10 6 5 10 12 5 6 4 5 2 7
10 11 13 11 14 13 12 15 14 13 16 15 14 15 13 14 15 14 13 12 15 14 16 15 14 14 13 14 15 18 19
15 16 17 15 16 18 17 16 15 16 18 17 16 19 20 21 22 19 18 17 19 16 17 29 21 22 24 23 25 27
20 25 26 25 24 25 27 26 25 28 27 24 25 26 27 28 27 26 25 26 27 26 28 30 31 25 26 33 28 29 30
27 26 25 24 25 20 29 27 23 24 25 20 21 23 24 23 22 21 23 24 21 23 25 26 25 24 23 23 21 20 19
19 18 17 11 15 16 17 16 15 14 15 14 16 15 14 13 15 16 15 14 15 16 15 14 17 16 16 15 11 10
12 10 8 7 9 8 7 6 5 6 7 6 5 6 7 8 9 8 7 4 5 6 7 6 5 7 8 7 6 8 6
4 3 5 3 4 3 2 3 4 3 2 -1 2 3 2 1 2 0 3 2 1 2 3 2 0 -3 -2 -1 -2 -1
-4 -5 -3 -5 -6 -5 -4 -5 -6 -7 -6 -5 -4 -3 -4 -5 -4 -3 -2 -6 -5 -4 -3 -19 -11 -12 -14 -13 -12 -8 -10


For Example, my program looks and has to look like this:


[[-7, -2, -4, -5, -12, -3, -6, -8, -9, -11, -15, -10, -10, -10, -19, -17, -25, -17, -10, -18, -14, -15, -14, -10, -9, -18, -19, -17, -13, -12, -10], [-9, -7, -6, -11, -15, -14, -16, -13, -12, -7, -6, -5, -12, -11, -7, -12, -13, -14, -16, -17, -14, -13, -13, -19, -20, -16, -15, -8], [-5, -4, -6, -5, -7, -6, -5, -4, -7, -6, -5, -4, -3, -5, -6, -4, -5, -3, -2, -1, -5, -4, -7, -4, -3, -2, -4, -3, -4, -2], [1, 2, 2, 3, 4, 3, 2, 4, 5, 6, 7, 5, 4, 5, 6, 7, 4, 8, 7, 10, 6, 5, 10, 12, 5, 6, 4, 5, 2, 7], [10, 11, 13, 11, 14, 13, 12, 15, 14, 13, 16, 15, 14, 15, 13, 14, 15, 14, 13, 12, 15, 14, 16, 15, 14, 14, 13, 14, 15, 18, 19], [15, 16, 17, 15, 16, 18, 17, 16, 15, 16, 18, 17, 16, 19, 20, 21, 22, 19, 18, 17, 19, 16, 17, 29, 21, 22, 24, 23, 25, 27], [20, 25, 26, 25, 24, 25, 27, 26, 25, 28, 27, 24, 25, 26, 27, 28, 27, 26, 25, 26, 27, 26, 28, 30, 31, 25, 26, 33, 28, 29, 30], [27, 26, 25, 24, 25, 20, 29, 27, 23, 24, 25, 20, 21, 23, 24, 23, 22, 21, 23, 24, 21, 23, 25, 26, 25, 24, 23, 23, 21, 20, 19], [19, 18, 17, 11, 15, 16, 17, 16, 15, 14, 15, 14, 16, 15, 14, 13, 15, 16, 15, 14, 15, 16, 15, 14, 17, 16, 16, 15, 11, 10], [12, 10, 8, 7, 9, 8, 7, 6, 5, 6, 7, 6, 5, 6, 7, 8, 9, 8, 7, 4, 5, 6, 7, 6, 5, 7, 8, 7, 6, 8, 6], [4, 3, 5, 3, 4, 3, 2, 3, 4, 3, 2, -1, 2, 3, 2, 1, 2, 0, 3, 2, 1, 2, 3, 2, 0, -3, -2, -1, -2, -1], [-4, -5, -3, -5, -6, -5, -4, -5, -6, -7, -6, -5, -4, -3, -4, -5, -4, -3, -2, -6, -5, -4, -3, -19, -11, -12, -14, -13, -12, -8, -10]]
Data has been received
Please enter a month : January
The most occurring temprature in January is : -10

So my question is how to put this to my code:?

Please enter a month :
The most occuring temprature is :

Edited 6 Years Ago by vegaseat: added code=text tags

You have done a fine job so far test printing your data.

Now let the user input a month with the correct spelling.
Index the month using:
mon_ix = months.index(mon_str)

That index matches the temperature data index, pull out he right list with:
mon_temps = temperatures[mon_ix]

Now create a temporary ft_list of (frequency, temperature) tuples using a loop and
ft_list.append((mon_temps.count(temperature), temperature))

Find the max tuple in the list with max(ft_list)

Extract the frequency and associated temperature from that tuple and display the result.

Again, keep test printing interim results to help you along.

Edited 6 Years Ago by vegaseat: n/a

This question has already been answered. Start a new discussion instead.