Python Forum
How to use python to do "for each 365 data, print the largest 18 value? - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: How to use python to do "for each 365 data, print the largest 18 value? (/thread-9802.html)



How to use python to do "for each 365 data, print the largest 18 value? - ctliaf - Apr-28-2018

I have a text file which shows the daily temperature of a specific location in 20 years. Therefore, there are total 365x20=7300 data. What should I do in python if I want to print only the largest 18 value per every 365 data? Therefore, the output file should have 18x20= 360 data.

As I am new to python, I appreciate it if a suggestion of a possible code including how to import and output the file could be provided.

Thank you very much.


RE: How to use python to do "for each 365 data, print the largest 18 value? - snippsat - Apr-28-2018

(Apr-28-2018, 07:25 PM)ctliaf Wrote: As I am new to python, I appreciate it if a suggestion of a possible code including how to import and output the file could be provided.
You have to show some effort as this is basic stuff,or you have to post it as job you want done.
I mean it's basic stuff like this:
with open('file_name.txt') as f:
    print(f.read()) # Whole file as string

with open('file_name.txt') as f:
    for line in f:
        # do something with line
Quote:if I want to print only the largest 18 value per every 365 data?
If you manger to get values in list from the text file.
>>> lst = [18, 10, 4, 4, -5, 20, 25, 32, 7]
>>> sorted(lst, reverse=True)[:3]
[32, 25, 20]
>>> 
>>> # Or
>>> import heapq
>>> heapq.nlargest(5, lst)
[32, 25, 20, 18, 10]