#๐Ÿ”’ Number sorting script

42 messages ยท Page 1 of 1 (latest)

solemn torrent
#

I knocked up this little script to put numbers in ascending order:

inputNums = input('Numbers: ')
inputNums = inputNums.rsplit(',')

outputNums = []

for counter in range(len(inputNums)):
outputNums.append(min(inputNums))
i = min(inputNums)
i = inputNums.index(i)
del inputNums[i]

print(outputNums)

It can take as many numbers as you give it, as long as they are seperated by a comma (',').
For some reason, it works perfectly only with numbers that all have the same amount of digits.
When you combine numbers of different amounts of digits, it seems unable to sort them properly.
It isn't anything to do with the number type, so I have absolutely no clue of why it doesn't work properly.

minor aspenBOT
#

@solemn torrent

Python help channel opened

Remember to:

  • Ask your Python question, not if you can ask or if there's an expert who can help.
  • Show a code sample as text (rather than a screenshot) and the error message, if you've got one.
  • Explain what you expect to happen and what actually happens.

:warning: Do not pip install anything that isn't related to your question, especially if asked to over DMs.

floral kiln
#

I think your code sorts strings, not integers

solemn torrent
#

how tho? doesn't the min() function only work with integers?

floral kiln
#

Your inputNums is a list of strings, even after rsplit

#

No, python can actually compare strings :)

#

And it even probably can sort them, but only character-by-character

solemn torrent
#

for counter in range(len(inputNums)):
i = 0
inputNums[i] = int(inputNums[i])

#

i added that but it threw an error

floral kiln
#

Why did you add this

solemn torrent
#

to convert each item in the list to an integer

#

it didn't work i think

floral kiln
#

It's not how it's usually done

solemn torrent
#

oh

#

im kind of a noob at python anyways ๐Ÿ˜Ž

floral kiln
#
inputNums = list(map(int, inputNums))```
To map an entire list to integers
solemn torrent
#

๐Ÿ‘€

floral kiln
#

Or you can do

for i in range(len(inputNums)):
    inputNums[i] = int(inputNums[i])```
#

That's just a longer way to do what i wrote above

hidden grotto
floral kiln
#

Yeah, your prior code sorted them as strings of digits by ascii table, so it could only properly compare equal-digit numbers

solemn torrent
floral kiln
#

You assigned i = 0 and didn't use counter at all, so it only converted the element at position 0

#

I named my "counter" "i" and used it to cycle through the entire list

solemn torrent
#

i think the problem with my one is that i didn't increment 'i' each loop

floral kiln
#

You don't need to

#

When you loop over range, python increments the "counter" for you

solemn torrent
#

oh

#

right

floral kiln
#

That's what range is for

solemn torrent
#

i never knew that

#

noted

#

thanks

floral kiln
#

Also what you name "counter", is usually named "i"

#

"i" stands for index, it's the index of the loop

solemn torrent
#

ye

floral kiln
#

But it's only a convention, not a rule, so you can name it whatever you want

rough cipher
floral kiln
#

Yeah, but imo a list comp is as understandable as a map, so decided to show this it a verbose way

minor aspenBOT
#
Python help channel closed

This help channel has been closed and it's no longer possible to send messages here. If your question wasn't answered, feel free to create a new post in #1035199133436354600. To maximize your chances of getting a response, check out this guide on asking good questions.