0

I have a text file containing the following data.

ascon1 201707011 John 77.5 11.5 11.5 11.5
ascon1 201707012 Grld 70.0 11.5 11.5 11.5
ascon1 201707013 Josh 79.5 11.5 11.5 11.5 
ascon1 201707014 Jess 67.5 11.5 11.5 11.5 
ascon1 201707015 Jack 97.5 11.5 11.5 11.5 

I need the data to look like this.

ascon1 201707015 Jack 97.5 11.5 11.5 11.5 
ascon1 201707013 Josh 79.5 11.5 11.5 11.5
ascon1 201707011 John 77.5 11.5 11.5 11.5 
ascon1 201707012 Grld 70.0 11.5 11.5 11.5 
ascon1 201707014 Jess 67.5 11.5 11.5 11.5 

so here is my code. The above part is working using the inbuilt sorted function from python. but in ascending order instead of decending order.

with open('input.txt') as f:
    lines = [line.split(' ') for line in f]
output = open("input(sorted).txt", 'w')

for line in sorted(lines, key=itemgetter(3)):
    output.write(' '.join(line))

output.close()

I want to use the quick-sort function to solve this problem. this is my function.

def qsort(d_val):
    if len(d_val) <= 1: return d_val
    return qsort([lt for lt in d_val[1:] if lt < d_val[0]]) + d_val[0:1] + \
           qsort([ge for ge in d_val[1:] if ge >= d_val[0]])
3
Contributors
10
Replies
57
Views
2 Months
Discussion Span
Last Post by pty
Featured Replies
  • The `sorted()` function has a boolean `reverse` parameter that allows to sort in descending order sorted(lines, key=itemgetter(3), reverse=True) This is much more efficient than trying to write your own quicksort method. Read More

1

The sorted() function has a boolean reverse parameter that allows to sort in descending order

sorted(lines, key=itemgetter(3), reverse=True)

This is much more efficient than trying to write your own quicksort method.

0

You can use > and <= in the qsort() function to sort in descending order. Is there a question with this function? You could add a key argument like so

def qsort(d_val, key=lambda x: x):
    if len(d_val) <= 1: return d_val
    return qsort([lt for lt in d_val[1:] if key(lt) > key(d_val[0]])) + d_val[0:1] + \
           qsort([ge for ge in d_val[1:] if key(ge) <= key(d_val[0]]))

There are other implementations, such as inplace implementations. One could also add a reverse parameter.

Edited by Gribouillis

0

when i run the code it is giving me an error.

return qsort([lt for lt in d_val[1:] if key(lt) > key(d_val[0]])) + d_val[0:1] + \
                                                                                                                                                ^

SyntaxError: invalid syntax

the square bracket in key(d_val[0]]))

0

This problem was solved like 40 years ago.

$ sort -t" " -rnk4 data.tsv
ascon1 201707015 Jack 97.5 11.5 11.5 11.5
ascon1 201707013 Josh 79.5 11.5 11.5 11.5
ascon1 201707011 John 77.5 11.5 11.5 11.5
ascon1 201707012 Grld 70.0 11.5 11.5 11.5
ascon1 201707014 Jess 67.5 11.5 11.5 11.5
  • -t" " to tell sort to split on spaces
  • -r for reverse (highest first)
  • -n to tell sort that it's a numeric field
  • -k4 to tell sort that we're loking at the fourth column
This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.