繁体   English   中英

在嵌套列表中的特定索引中实施选择排序

[英]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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM