-1

This is my first time asking for help here, but surely not last since I'm just a struggling beginner.

I have a text file with some numbers eg. :

5 26 83 1 5645 

7 3758 69 4 84

I want to sort these lines into lists, which I have partially done with this line of code: result = [line.split(" ") for line in file.readlines()] - not sure if this is the right way to do it in order to work with it but maybe I'll find some help here

Now, when I have every line of the text file separated as a list of values, I want to find the highest value in the whole text file and find in which line the number is. - I can find the highest value with "max" method but that's about it

For example, out of 20 lines of numbers, the highest value is "94515" and is located in 13th row.

That's what I would like to accomplish.

Arkistarvh Kltzuonstev
  • 6,824
  • 7
  • 26
  • 56
sajpy
  • 3
  • 1
  • 1
    Why do you need to retain all the values in your list by using comprehension? Just read line by line, note down max value and line number if its greater than the biggest till now? You don't need to sort as well if all you need is biggest number. – SMA Nov 24 '19 at 14:09
  • The title reads `Sorting …` - can you please explain what this does refer to? (Or have you intended *splitting*?) – greybeard Nov 24 '19 at 14:11
  • @greybeard what I was referring to was putting every line of code into a list, probably should have used a different word, sorry – sajpy Nov 24 '19 at 14:23
  • @SMA I just thought that putting the values into lists would somehow make it easier for me, but what you are saying makes sense, thank you – sajpy Nov 24 '19 at 14:24

2 Answers2

0

Try this:

with open('path/to/file', 'r') as file:
    result = [list(map(int, line.split())) for line in file.readlines() if line.strip()]
max_int = max(j for i in result for j in i)
line_nums = [i+1 for i, j in enumerate(result) if max_int in j]
Arkistarvh Kltzuonstev
  • 6,824
  • 7
  • 26
  • 56
  • Thank you. It works very well, the problem occured when I added an empty line in the file, the code doesn't register it. Could you please comment on the process of the code? I'm kind of lost tbh – sajpy Nov 24 '19 at 14:36
0

Processing line by line:

with open('data.txt', 'r') as file: 
  imax, max_line = float('-inf'), -1    # init of overall max and line number
  for line_no, line in enumerate(file):
    # Use try/except to ignore problem lines (such as blank line)
    try:
      line_max = max(map(int, line.rstrip().split()))  # max in line
                     # Line above does the following:
                     # line.strip() remove "/n" at end of line
                     # .split() convert string into list of numbers
                     # (e.g. "1 2 3".split() -> ['1', '2', '3']
                     # map(int, lst) converts list of strings into numbers
                     # (i.e. ['1', '2', '3'] -> [1, 2, 3]
                     # max takes the max of the numbers in the list
      if line_max > imax:
        imax, max_line = line_max, line_no        # update overall max
    except:
      continue                     # ignores problems (such as blank lines)

print(f'Max {imax} on line {max_line}')  

Test

data.txt file contents (blank third line)

5 26 83 1 5645
7 3758 69 4 84

17 8 19 21 15
232 231 99999 15

Output (outputs max value and line number)

Max 99999 on line 4
DarrylG
  • 16,732
  • 2
  • 17
  • 23
  • Well yes, it works and thank you for that! But what if I don't wanna ignore blank lines? Because the number 99999 isn't really in line 4, but line 5. – sajpy Nov 24 '19 at 15:01
  • I could just do `max_line += 1` but that's true only if the number of blank lines is 1.. I should count number of blank lines and add it to "max_line" right ? – sajpy Nov 24 '19 at 15:03
  • The blank line is being counted in the line numbering. enumerate by default creates an index that starts at 0 (so first line is line 0). That can be easily changed with enumerate(file, start = 1). This would make line_no in the loop automatically go through the sequence 1, 2, 3, etc. as we loop through lines. [More details](https://www.programiz.com/python-programming/methods/built-in/enumerate) – DarrylG Nov 24 '19 at 15:04
  • Riiight..Thank you! That helped a lot. – sajpy Nov 24 '19 at 15:06
  • @DarryIG Could you please describe what does this do ? `imax, max_line = float('-inf'), -1` Especially the part **('-inf'), -1** is causing me trouble to undestand. You say it's init but only init I know so far is the __init__ class and that's not it, right ? – sajpy Nov 24 '19 at 16:22
  • @sajpy--In python 2, the standard way to initialize a value to a large positive or negative number is to make use of sys.maxint , with a +/- sign in front. Python 3 removed sys.maxint from the language, so in Python 3 it's popular to use float('inf') (for large positive) or float('-inf') (for large negative) numbers [See](https://stackoverflow.com/questions/2919754/how-do-i-type-a-floating-point-infinity-literal-in-python). Also, a, b = 10, 20 is equivalent to a = 10, c = 20 [more details](https://treyhunner.com/2018/03/tuple-unpacking-improves-python-code-readability/) – DarrylG Nov 24 '19 at 16:37
  • Interesting.. I think I get it now. Thank you for your help :) – sajpy Nov 24 '19 at 16:45