我正在研究一個程序,該程序需要一個列表並將其從最高到最低排序[是的,我知道我可以使用.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