0

I have text file with parse logged file, file contains: file: output.log End result should be: 1. sorted the list with max number

rt01,te1/1,111,11 rt02,te1/1,11,10 rt01,te1/2,122,20 rt02,te1/2,113,5 rt02,te1/3,10,1 rt03,te1/1,1,6 rt03,te1/2,11,8

Result:

rt01,te1/2,122,20 rt01,te1/1,111,11 rt02,te1/1,11,10 rt03,te1/2,11,8 rt03,te1/1,1,6 rt02,te1/2,113,5 rt02,te1/3,10,1 What is the best way, to get the max value within the list: I try:

results = []
top = []
bottom = []
found = 0
with open('Python/output.log', 'r') as f:
for line in f:
    line = line.split(",")
    results.append(line)
print results

for i,j in enumerate(results[:-1]):
    #print results[i+1][3]
    if j[3] > results[i+1][3]:
        top.append(j)
    elif results[i+1][3] > top[3]:
        bottom.append(results[i+1])
        if top[i-1][3] > results[i+1][3]:
            top.append(j.pop())
            bottom.append(j)
            #top.append(j[])

print(top)
print(bottom)
netkool
  • 45
  • 1
  • 11
  • "What is the best way, to get the max value within the list?" `max(my_list)`. For the rest I have no clue what you are trying to do... – Julien Apr 12 '18 at 01:48
  • What's wrong with what you tried? You can certainly make it a lot simpler—use `csv.reader(f)`, call `max` directly on that with a `key` function, etc.—but that may not make it easier to understand. – abarnert Apr 12 '18 at 01:48
  • hey abarnert, I guess i was making complicated – netkool Apr 12 '18 at 03:59

3 Answers3

1

You can use the last number in each line as the key to sorted():

with open('test.txt') as f:
  data = f.read().splitlines()
  data = sorted(data, key= lambda x: int(x.split(',')[-1]), reverse=True)
  print('\n'.join(data))

Output:

rt01,te1/2,122,20
rt01,te1/1,111,11
rt02,te1/1,11,10
rt03,te1/2,11,8
rt03,te1/1,1,6
rt02,te1/2,113,5
rt02,te1/3,10,1
user3483203
  • 50,081
  • 9
  • 65
  • 94
  • I try to do sorted but somehow was getting int error. Thanks for the solution. How is it sorted via last value? – netkool Apr 12 '18 at 04:05
0

My own solution, similar to @chrisz with sorted() but using the csv library

import csv

results = []

with open('Python/output.log', 'r') as f:
    csvreader = csv.reader(f)
    for line in csvreader:
        results.append(line)

sorted_results = sorted(results, key=lambda x: int(x[3]), reverse=True)

Outputs:

[['rt01', 'te1/2', '122', '20'],
 ['rt01', 'te1/1', '111', '11'],
 ['rt02', 'te1/1', '11', '10'],
 ['rt03', 'te1/2', '11', '8'],
 ['rt03', 'te1/1', '1', '6'],
 ['rt02', 'te1/2', '113', '5'],
 ['rt02', 'te1/3', '10', '1']]
ScottMcC
  • 4,094
  • 1
  • 27
  • 35
0

This is a variant of a natural sort. If you are ONLY grouping by the final number in a CSV line, you can split on the ',', convert that to an int and then negate to get max->min sorting:

s='''\
rt01,te1/1,111,11
rt02,te1/1,11,10
rt01,te1/2,122,20
rt02,te1/2,113,5
rt02,te1/3,10,1
rt03,te1/1,1,6
rt03,te1/2,11,8'''

print '\n'.join(sorted(s.splitlines(), key=lambda s: -int(s.split(',')[-1])))

Prints:

rt01,te1/2,122,20
rt01,te1/1,111,11
rt02,te1/1,11,10
rt03,te1/2,11,8
rt03,te1/1,1,6
rt02,te1/2,113,5
rt02,te1/3,10,1

Since Python uses a stable sort, the groups in the first columns will remain in file order unless sorted by the final column.

If you want to sort BOTH by the final column and the numeral in the first column, you can use a regex and do something like:

import re

def f(line):
    digits=map(int, re.findall(r'\d+', line))
    return digits[0], -digits[-1]

print '\n'.join(sorted(s.splitlines(), key=f))

Prints:

rt01,te1/2,122,20
rt01,te1/1,111,11
rt02,te1/1,11,10
rt02,te1/2,113,5
rt02,te1/3,10,1
rt03,te1/2,11,8
rt03,te1/1,1,6

Or, if you want to sort primarily by the final column but group by the first column you can reverse the order of the tuple returned by f:

def f(line):
    digits=map(int, re.findall(r'\d+', line))
    return -digits[-1], digits[0]

print '\n'.join(sorted(s.splitlines(), key=f))
dawg
  • 98,345
  • 23
  • 131
  • 206