Python Forum
how to get the highest monthly average temperature?
Thread Rating:
  • 1 Vote(s) - 3 Average
  • 1
  • 2
  • 3
  • 4
  • 5
how to get the highest monthly average temperature?
#1
my question here
Create a program that reads from a file to display city name and average temperature in Celsius

use !curl to download https://raw.githubusercontent.com/Micros...p_mean.csv as mean_temp.txt

1) open the file in 'r' mode
2) read the first line of text into a variable called: headings and print()
3) convert headings to a list using .split(',') which splits on each comma, print() the list
4) use a while loop to read the remaining lines from the file
5) assign remainging lines to a city_temp variable
6) convert the city_temp to a list using .split(',') for each .readline() in the loop
7) print each city & the highest monthly average temperature
8)close mean_temps

!curl https://raw.githubusercontent.com/MicrosoftLearning/intropython/master/world_temp_mean.csv -o mean_temp.txt
mean_file=open("mean_temp.txt","r")
headings=mean_file.readline().strip('\n')
print(headings)
heading=headings.split(",")
print(heading)
city_temp=""
city_temp2=[]
headings2=mean_file.readline()
while headings2:
    city_temp+=headings2
    city_temp2.append(headings2.split(","))
    headings2=mean_file.readline()
    
print(city_temp)
print(city_temp2)
for city in city_temp2:
    print(city[0])
Output:

city,country,month ave: highest high,month ave: lowest low
['city', 'country', 'month ave: highest high', 'month ave: lowest low']
Beijing,China,30.9,-8.4
Cairo,Egypt,34.7,1.2
London,UK,23.5,2.1
Nairobi,Kenya,26.3,10.5
New York City,USA,28.9,-2.8
Sydney,Australia,26.5,8.7
Tokyo,Japan,30.8,0.9

[['Beijing', 'China', '30.9', '-8.4\n'], ['Cairo', 'Egypt', '34.7', '1.2\n'], ['London', 'UK', '23.5', '2.1\n'], ['Nairobi', 'Kenya', '26.3', '10.5\n'], ['New York City', 'USA', '28.9', '-2.8\n'], ['Sydney', 'Australia', '26.5', '8.7\n'], ['Tokyo', 'Japan', '30.8', '0.9\n']]
Beijing
Cairo
London
Nairobi
New York City
Sydney
Tokyo

But how to get the highest monthly average temperature?
Reply
#2
you have list of lists.
each city is list with 4 elements - city, country, highest high average, lowest low average.
you need just 1 and 3 element of each list
Reply
#3
You should use the module csv:
import csv


with open('world_temp_mean.csv') as fd:
    reader = csv.reader(fd)
    head = next(reader)
    data = list(reader)
Now you have data, which is a list. Each element in the list is one row of the data.
You can do an inline sort of the list. If you want to have different sorted new lists, you should use the built-in function sorted().

from operator import itemgetter


# avg highest tmp is in index 2
# sort by index 2
data.sort(key=itemgetter(2), reverse=True)
# or with lambda
data.sort(key=lambda x: x[2], reverse=True)
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  My first temperature converter TheLowEndTheory 7 3,337 Oct-18-2020, 04:39 PM
Last Post: buran
  Monthly pollution rate RbaPhoenix 4 2,641 Mar-28-2020, 04:01 PM
Last Post: ibreeden
  Monthly report for pushed buttons chano 2 2,089 Oct-09-2019, 01:40 PM
Last Post: chano
  Methods that return the highest score from the list erfanakbari1 7 7,053 Mar-26-2019, 08:32 PM
Last Post: aankrose
  Temperature converter Help (ASAP) Edison_Weng 1 2,790 Apr-16-2018, 01:55 PM
Last Post: stranac
  Returning the highest value out of 3 values ComputerSkillet 2 3,797 Apr-28-2017, 03:16 AM
Last Post: volcano63
  Numerically determining the highest a ball travels when shot straight up JakeWitten 3 3,378 Apr-22-2017, 04:37 PM
Last Post: Ofnuts

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020