r/programmingrequests • • 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

38 comments sorted by

2

u/BitwiseShift Sep 12 '16

As fast as you'll get it, most of the time is wasting reading and writing the files:

import sys

if len(sys.argv) != 2 or not (sys.argv[1] == "asc" or sys.argv[1] == "desc"):
	print('Usage: "{0} asc" for ascending order "{0} desc" for descending sort.'.format(sys.argv[0]))
else:
	desc = sys.argv == "desc"
	nums = [float(x) for x in open("in.txt").readlines()]
	nums_sorted = sorted(nums, reverse=desc)

	f = open("out.txt", "w")
	for num in nums_sorted:
		f.write(str(num)+"\n")

2

u/nyslyetia Sep 12 '16

I would add the following:

f.write(str(num).rstrip('0').rstrip('.')+"\n")

1

u/micmic0000 Sep 12 '16

Hi

I tried to add the following line

hehe2.jpg

either this line only -

f.write(str(num).rstrip('0').rstrip('.')+"\n")

or both

f.write(str(num)+"\n") f.write(str(num).rstrip('0').rstrip('.')+"\n")

and no out.txt file will come up on the same directory.

1

u/nyslyetia Sep 12 '16

Do you need to sort the numbers in each line or in each file?

1

u/micmic0000 Sep 12 '16

we can have it by ascending order or descending order or both so

the results would be like this: thanks

hehe3.jpg

2

u/nyslyetia Sep 12 '16

try this:

import sys

def formatNumbers(a):
    return str(a).rstrip('0').rstrip('.')

if len(sys.argv) != 2 or not (sys.argv[1] == "asc" or sys.argv[1] == "desc"):
    print('Usage: "{0} asc" for ascending order "{0} desc" for descending sort.'.format(sys.argv[0]))
else:
    desc = sys.argv == "desc"
    lines = open("in.txt").readlines()
    f = open("out.txt", "w")

    for l in lines:
        nums = [float(x) for x in l.split('-')]
        nums_sorted = sorted(nums, reverse=desc)
        nums_sorted = map(formatNumbers, nums_sorted)
        f.write('-'.join(nums_sorted)+"\n")

1

u/micmic0000 Sep 12 '16

Hi nyslyetia,

Im still getting error - hehe4.jpg

btw im using Python 3.4.3

1

u/nyslyetia Sep 12 '16

you have to call the script with an argument "asc" or "desc" and check the indentation

1

u/micmic0000 Sep 12 '16

Hi nyslyetia,

not sure what you mean by to call where do i need to assign the "asc" or "desc" on your code?

Thanks

2

u/nyslyetia Sep 13 '16

if you're using python IDLE try this instead and define the "order" variable:

import sys

#options: "asc" or "desc"
order = "asc"

def formatNumbers(a):
    return "{:n}".format(a)

desc = order == "desc"
lines = open("in.txt").readlines()
f = open("out.txt", "w")

for l in lines:
    nums = [float(x) for x in l.split('-')]
    nums_sorted = sorted(nums, reverse=desc)
    nums_sorted = map(formatNumbers, nums_sorted)
    f.write('-'.join(nums_sorted)+"\n")
→ More replies (0)

1

u/BitwiseShift Sep 12 '16

I like the added formatting. Although I think

"{:n}\n".format(num)

will do the same thing, except that it's already built in.

1

u/micmic0000 Sep 12 '16

btw the program is not working, im using Python 3.4.3

i change this line: nums = [float(x) for x in open("in.txt").readlines()] to

nums = [float(x) for x in open("C:/Users/mlee1056/Desktop/R/Sort/in2.txt").readlines()]

and change this line: f = open("out.txt", "w")

to

f = open("C:/Users/mlee1056/Desktop/R/Sort/out2.txt", "w")

1

u/BitwiseShift Sep 12 '16

That's because it assumes that in.txt and out.txt are in the same directory as the script. But I'm glad to hear you've got it working.

1

u/micmic0000 Sep 12 '16

hi

i have the in.txt and Sort - [Up and Down].py on the same directory but when i run the program no out.txt will come up

i even created out.txt file on the same directory no results comes up on the out.txt

1

u/BitwiseShift Sep 12 '16

Hm, it's working for me. How is your input formatted? 1 number per line? Separated by commas? Could you give me a couple of numbers as they are in your in.txt.

1

u/micmic0000 Sep 12 '16

Hi

my numbers inside does not have any commas

please refer to the attached screenshot hehe.jpg

:)

1

u/BitwiseShift Sep 12 '16

And how should this be sorted? Each line should be sorted?

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))

:)