[英]Implement Selection Sort in a specific index in a nested list
I am able to implement selection sort for every first value of a list, so eg list[0][0]
, list[1][0]
, list[2][0]
, list[3][0]
我能够为列表的每个第一个值实现选择排序,因此例如list[0][0]
, list[1][0]
, list[2][0]
, list[3][0]
I want to implement a sort so that it sorts through the second value of every list, so eg list[0][1],list[1][1],list[2][1],list[3][1]
我想实现一个排序,以便对每个列表的第二个值进行排序,例如list[0][1],list[1][1],list[2][1],list[3][1]
This is my list: 这是我的清单:
[[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]]
When sorted it will be like this: 排序时将如下所示:
[[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]]
I want it to sort the second value of the list. 我希望它对列表的第二个值进行排序。 How should i change my code to accomplish this? 我应该如何更改我的代码以实现此目的? Any help is appreciated. 任何帮助表示赞赏。
This is my sorting algorithm: 这是我的排序算法:
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.