我正在研究一个程序,该程序需要一个列表并将其从最高到最低排序[是的,我知道我可以使用.sort()]。该方案如下:Python 2:排序算法不起作用
UnList = raw_input("Enter a list of numbers seperated by commas") #collects the users list
List = UnList.split(",") #Turns the users numbers into a list
for x in range(len(List)): #number of time to sort
Switcher = 0 #the switcher (also will reset it later)
for z in range(len(List)-1): #number of time the switcher runs
if List[Switcher] > List[(Switcher + 1)]: #if one number is bigger than the other
List[Switcher],List[(Switcher+1)] = List[(Switcher+1)],List[Switcher] #this switches those 2 numbers
Switcher += 1 #adds to the switcher
print "The new list is:", List #prints the output
有时它的工作原理,如与榜样“1,7,4,6,3” 其他时候,如用“-10,5,4,32, 4,-40,2“会给出完全不正确的输出”['-10','-40','2','32','4','4','5']“
你的值是字符串而不是整数,因此''''出现在'-40'之前 – Jaco
Nitpicky与问题无关的东西,约定是以小写字母开头而不是大写 – StephenTG