[英]Implement Selection Sort in a specific index in a nested list
我能夠為列表的每個第一個值實現選擇排序,因此例如list[0][0]
, list[1][0]
, list[2][0]
, list[3][0]
我想實現一個排序,以便對每個列表的第二個值進行排序,例如list[0][1],list[1][1],list[2][1],list[3][1]
這是我的清單:
[[130, 266.07], [46, 174.14], [169, 187.01], [179, 488.69], [53, 401.53], [128, 106.88], [97, 398.33], [152, 493.87], [20, 205.43], [94, 248.14]]
排序時將如下所示:
[[20, 205.43], [46, 174.14], [53, 401.53], [94, 248.14], [97, 398.33], [128, 106.88], [130, 266.07], [152, 493.87], [169, 187.01], [179, 488.69]]
我希望它對列表的第二個值進行排序。 我應該如何更改我的代碼以實現此目的? 任何幫助表示贊賞。
這是我的排序算法:
def swapElements(myList,i,j):
temp = myList[i]
myList[i] = myList[j]
myList[j] = temp
def getMinIndex(list,start,stop):
minIndex = start
for j in range(start,stop):
if list[j]<list[minIndex]:
minIndex = j
return minIndex
def selectionSort(list):
for i in range(len(list)):
minIndex = getMinIndex(list,i,len(list))
swapElements(list, i, minIndex)
print(list)
def SelectionSortPrice(list):
selectionSort(list)
#printList(list) ##part of a different function to format the list, irrelevant
SelectionSortPrice(x)
sorted(yourlist, key= lambda num: num[1])
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.