Python 代码悬疑

22.选择排序是另一种简单的排序方法。假设长度为n的列表lst,要按照从小到
大排序,则选择排序的具体过程可以描述为:首先从n个数字中找到最小值min1,
如果最小值min1的位置不在列表的最左端(也就是min1不等于lst[0]),则将最
小值min1和lst[0]交换,接着在剩下的n-l个数字中找到最小值min2,如果最小
值min2不等于lst[1],则交换这两个数字,依次类推,直到列表lst有序排列。
请给定任一列表lst实现以上过程。(不能使用python自带的min,max函数)23.读入一个字符串,内容为英文文章,返回其中出现次数最多的单词(仅返回

22

def selection_sort(lst):
    n = len(lst)
    for i in range(n):
        min_index = i
        for j in range(i+1, n):
            if lst[j] < lst[min_index]:
                min_index = j
        lst[i], lst[min_index] = lst[min_index], lst[i]
    return lst

23

def find_most_common_word(text):
    # 将文章中的标点符号替换为空格,然后分割成单词列表
    words = text.lower().replace(",", " ").replace(".", " ").split()
    word_count = {}
    for word in words:
        if word in word_count:
            word_count[word] += 1
        else:
            word_count[word] = 1
    # 找出出现次数最多的单词
    most_common_word = max(word_count, key=word_count.get)
    return most_common_word