Home > Net >  How do you sort input in Python?
How do you sort input in Python?

Time:10-27

beginning coder here. I was trying to sort words and numbers, and it mostly work. However, when I provided input to sort, it doesn't seem to work.

I tried inserting the input as a variable and it broke the numbers down into single digits, which I did not want to do. I tried converting the input into a float and integers and it did not work. I tried

input1 = input() #I input in 23, 12, 45
sorted(input1)

And I got

[' ', ' ', ',', ',', '1', '2', '2', '3', '4', '5']

I was expecting for it to sort by whole numbers without being broken down.

CodePudding user response:

If your input is a String, you can convert it to int and add to a list. Then you can sort the list:

list =  []
x = input("write:")
x = x.replace(" ","")
y = x.split(",")
for i in y:
    if i.isnumeric():
        i = int(i)
    list.append(i)
list.sort()
print(list)
  • Related