[Solved] sort a field in ascending order and delete the first and last number [closed]


Python:

with open('the_file.txt', 'r') as fin, open('result.txt', 'w') as fout:
    for line in fin:
        f0, f1 = line.split() 
        fout.write('%s\t%s\n' % (f0, ','.join(sorted(f1.split(','), key=int)[1:-1])))

The body of the loop can be unpacked as:

        f0, f1 = line.split()           # split fields on whitespace
        items = f1.split(',')           # split second field on commas
        items = sorted(items, key=int)  # or items.sort(key=int) # sorts items as int
        items = items[1:-1]             # get rid of first and last items
        f1 = ','.join(items)            # reassemble field as csv
        line="%s\t%s\n" % (f0, f1)    # reassemble line
        fout.write(line)                # write it out

4

solved sort a field in ascending order and delete the first and last number [closed]