![](/img/trans.png)
[英]How do I sort a list in ascending order using a specific column, without using a build in function? (Python)
[英]python program to sort a list in ascending order without using the sort function
//它只適用於數字 //我希望它也適用於字符串
def method(list):
for i in range(len(list)):
for j in range(i+1,len(list)):
if list[i]>list[j]:
list[i],list[j]=list[j],list[i]
return list
您的代碼看起來不錯,並且還能夠對字符串列表進行排序。
你只需要做一些小的修復。 試試下面的代碼:
def my_sorting(input_list):
for i in range(len(input_list)):
for j in range(i+1,len(input_list)):
if input_list[i] > input_list[j]:
input_list[i],input_list[j] = input_list[j],input_list[i]
return input_list
樣本輸入/輸出:
輸入---> input_list = ["zas", "app", "zaa", "abc", "aaz"]
Output ---> ['aaz', 'abc', 'app', 'zaa', 'zas']
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.