r/programmingrequests • u/micmic0000 • Sep 12 '16
[REQUEST] Python that will sort numbers in ascending or descending order
Hi,
Can help make this program in python? the one that is just like in Excel that you sort number. the problem with Excel it can handle only 1.4 mil numbers only and i found that notepad can handle more than 14 mil + numbers
the program would like be me having the in.txt wit all my numbers and the program will generate out.txt that is already sorted out either ascending or descending order
Thanks :)
3
Upvotes
1
u/serg06 Sep 14 '16
For Python 2.7:
inputfile = 'C:/in.txt'
outputfile = 'C:/out.txt'
outputfile_reversed = 'C:/out_reversed.txt'
with open(inputfile,'r') as f:
lines = [l.strip() for l in f.read().replace('\n',' ').strip().split() if l != '']
lines.sort(key=lambda x: [int(n) for n in x.split('-')])
with open(outputfile,'w') as f:
f.write('\n'.join(lines))
lines.reverse()
with open(outputfile_reversed,'w') as f:
f.write('\n'.join(lines))There's a slight complication with writing a "new line" key on windows - if the program prints it all on one line, change this in the code
f.write('\n'.join(lines))to this
f.write('\r\n'.join(lines)):)
2
u/BitwiseShift Sep 12 '16
As fast as you'll get it, most of the time is wasting reading and writing the files: