Python Forum
Search for the line number corresponding to a value
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Search for the line number corresponding to a value
#1
Hello,

I have a.csv file with two columns. I search in one of the columns for a value approximating "K" using the following script:

Re=np.loadtxt('./Re.CSV',delimiter=',').T
def closest(lst, K): 

     lst = np.asarray(lst) 
     idx = (np.abs(lst - K)).argmin() 
     return lst[idx] 

# Driver code 
lst = Re[0]
K=72
print(closest(lst, K))
Now, I would like to have the line number corresponding to the approximate value of K, how could I get it back? Do you have any idea?
Thank you,
Reply


Messages In This Thread
Search for the line number corresponding to a value - by Lali - Oct-22-2019, 08:56 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Sequential number for rows retrieved and storing the Primary UKey to the line number GYKR 2 628 Aug-22-2023, 10:14 AM
Last Post: GYKR
  Search text in PDF and output its page number. atomxkai 21 9,267 Jan-21-2022, 06:20 AM
Last Post: snippsat
  Search Results Web results Printing the number of days in a given month and year afefDXCTN 1 2,277 Aug-21-2020, 12:20 PM
Last Post: DeaD_EyE
  line number of first and second occurance of string in a file mdalireza 1 1,868 Nov-18-2019, 09:55 AM
Last Post: perfringo
  print number of a list line per line lateublegende 2 2,761 Mar-20-2019, 04:07 PM
Last Post: lateublegende
  Adding a line number to an lxml Element vindy 0 3,412 Mar-08-2019, 08:34 PM
Last Post: vindy
  get the number in the line in text file lateublegende 2 2,538 Jan-29-2019, 06:03 PM
Last Post: lateublegende
  Help with python code to search string in one file & replace with line in other file mforthman 26 12,110 Dec-19-2017, 07:11 PM
Last Post: Larz60+
  getting the source line number Skaperen 12 7,964 Dec-17-2017, 04:29 AM
Last Post: Skaperen
  search and replace with line in document chickflick91 2 3,393 Oct-15-2017, 06:33 AM
Last Post: chickflick91

Forum Jump:

User Panel Messages

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