Python Forum
How to use python to do "for each 365 data, print the largest 18 value?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to use python to do "for each 365 data, print the largest 18 value?
#1
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.
Reply
#2
(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]
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  XML minidom "Pretty Print" Lost Data marksy95 2 257 Jun-15-2024, 11:09 AM
Last Post: Larz60+
  print(data) is suddenly invalid syntax db042190 6 1,463 Jun-14-2023, 02:55 PM
Last Post: deanhystad
Question Help to find the largest int number in a file directory SalzmannNicholas 1 1,744 Jan-13-2022, 05:22 PM
Last Post: ndc85430
  find 2 largest equal numbers Frankduc 13 3,929 Jan-11-2022, 07:10 PM
Last Post: Frankduc
  how to print all data from all data array? korenron 3 2,603 Dec-30-2020, 01:54 PM
Last Post: korenron
  Largest product in a grid (projecteuler problem11) tragical 1 2,389 Sep-14-2020, 01:03 PM
Last Post: Gribouillis
  Extract the largest value from a group without replacement (beginner) preliator 1 2,188 Aug-12-2020, 01:56 PM
Last Post: DPaul
  frequency of largest number group anshumanmuj 5 3,160 Jun-22-2020, 04:51 PM
Last Post: perfringo
  Sort by the largest number of the same results (frequency) inlovewiththedj 3 2,350 Apr-01-2020, 07:29 AM
Last Post: DPaul
  How to print counter without bracket in python and sort data. phob0s 1 2,889 Jul-25-2019, 05:33 PM
Last Post: ichabod801

Forum Jump:

User Panel Messages

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