如何在列表中实现选择排序?

How to implement Selection Sort within a list?

所以我有一个 .txt 文件如下:

131,263.07
47,170.14
170,190.01
180,412.69
53,401.53

我必须读取文件才能输出如下列表:

131 kms, 3.07
47 kms, 0.14
170 kms, 0.01
180 kms, 2.69
53 kms, 1.53

我使用的代码是:

def PrintList(table):
    for line in table:
       print(str(line[0]) + " kms, $" + str(line[1]))

file = open(input("Enter file name: ")) 

table = []
for line in file:
    line = line.rstrip().split(",")
    line[0] = int(line[0])
    line[1] = float(line[1])
    table.append(line)

PrintList(table)

file.close()

现在我想按价格递增的顺序对列表进行排序以获得:

47 kms, 0.14
170 kms, 0.01
131 kms, 3.07
53 kms, 1.53
180 kms, 2.69

我如何在 Python 中实现它?我已经尝试使用 Selection Sort 来执行此操作,但它似乎不起作用。

更新:感谢您到目前为止的投入。但是,我已经尝试了排序功能,但我想弄清楚如何使用 Selection Sort.

来实现它

更新: 我无法 post 我使用的 Selection Sort 代码,因为我已经覆盖了它,但下面给出了一个示例我必须修改代码(我用来对随机距离列表进行排序),以便按价格递增的顺序对上述列表进行排序。希望足够了。

def selectionSort(distance):
    n = len(distance)
    for i in range(n):
        minPlace = searchMin(distance)
        swap(distance, i, minPlace+i)

def searchMin(distance):
    minPlace = 0
    n = len(distance)
    for i in range(1, n):
        if distance[i] < distance[minPlace]:
            minPlace = i
        return minPlace

def swap(distance, i, j):
    temp = distance[i]
    distance[i] = distance[j]
    distance[j] = temp

如果有更简单的实现方法,请告诉我。提前致谢。干杯。

是否一定要实现选择排序?我会选择 sorted:

PrintList(sorted(table, key = lambda x: x[1]))

哪里

key = lambda x: x[1]

指示sorted使用索引为1(价格)的元素的值来比较您的对象。

在你的 for 循环之后:

sorted_table = sorted(table, key=lambda row: row[1], reverse=True)

Sorted_table 现在包含您的表格数据,按第 2 列排序。 然后您可以将 sorted_table 传递给您的 PrintList 函数。避免 lambda 的替代方法:

from operator import itemgetter
sorted_table = sorted(table, key=itemgetter(1), reverse=True)

有关 python 排序的更多信息,请参阅:https://wiki.python.org/moin/HowTo/Sorting

Python 列表已经带有 sort 方法。您可以简单地调用它,指定一个 key 参数来确定如何排序。

def print_list(table):
    for line in table:
       print(str(line[0]) + " kms, $" + str(line[1]))

with open(input("Enter file name: ")) as f:
    table = []
    for line in f:
        line = line.rstrip().split(",")
        line[0] = int(line[0])
        line[1] = float(line[1])
        table.append(line)

    table.sort(key=lambda line: line[1])
    print_list(table)

请注意,我对您的程序做了一些额外的更改,即根据 PEP8, and using the with statement 重命名 PrintList 以便文件自动关闭。

如果坚持使用选择排序(会比Python的默认排序差),在实现sorted.

接口的辅助函数中实现

暴力选择排序

lst = [56,2,5,3,6,4,7,1,8,10,34]
print(lst)

#Total length of the list
leng=len(lst)

#Loop from 1st to last of the list
for i in range(leng):
    #Assume first value as minimum 
    min = lst[i]
    print(min,'Minimum value before the for loop')
    #Scan through every value for checking the minimum 
    for j in range(i,leng):
        if lst[j] < min:          
            min=lst[j]
            ts=j
            print(min,"Minimum value", ts, "Is it's corresponding index")
    #We have now the new minimum value and it's corresponding index.
    #Swap the new minimum value and the corresponding index            
    if min <lst[i]:
         temp =lst[i]
         lst[i]=lst[ts]   
         lst[ts]=temp
    print(lst,'Outer list')