Python sort selection

Here's the sort sort code, but it doesn't print the sorted list. How can I show it?

badlist = input("Enter list: ") def select(badlist): l = list[:] sorted = [] while len(l): lowest == l[0] for x in l: if x < lowest: lowest = x sorted.append(lowest) l.remove(lowest) return sorted select(badlist) 
0
source share
3 answers

Use print .

If you are using Python 2.x print , this is the keyword:

 result = select(badlist) print result 

There is a function in Python 3.x print , and you should use parentheses:

 result = select(badlist) print(result) 

You also have at least two other errors:

  • Your functional parameter is called badlist , but you never use it.
  • The == operator is an equality comparison. You need = for the destination.

Also your algorithm will be very slow, requiring O (n 2 ) operations.

0
source

if you type select(badlist) in the Python shell, it should show the result, however, if you use your script as a file, you need to use the print statement, like print select(badlist) .

+2
source

Extension of my comment:

Why not just use the built-in sorted(list) , and then you can just have all your code:

print sorted(input("Enter list: "))

+1
source

All Articles