使用python函数进行基数排序

时间:2014-10-17 05:59:27

标签: python sorting radix-sort

我正在使用此代码在python中使用基数排序对列表进行排序

def radix(self, a):
        len_a = len(a)
        modulus = 10
        div = 1
        while True:
            '''DECLARATION OF BUCKETS'''
            new_list = [[], [], [], [], [], [], [], [], [], []]
            for value in a:
                least_digit = value % modulus
                least_digit /= div
                new_list[least_digit].append(value)
            modulus = modulus * 10
            div = div * 10

            if len(new_list[0]) == len_a:
                '''RETURN THE LIST WHEN SORTED'''
                return new_list[0]

            a = []

            rd_list_append = a.append

            for x in new_list:
                for y in x:
                    rd_list_append(y)

我无法理解这些行的作用

            a = []

            rd_list_append = a.append

            for x in new_list:
                for y in x:
                    rd_list_append(y)

我知道for循环是如何工作的但是是什么 rd_list_append(y)的 请帮我。我是python的新手。

1 个答案:

答案 0 :(得分:0)

好的,这就是为什么他们告诉你不要从互联网上复制代码作为你的作业。

这部分代码基本上是修改原始列表以进行部分排序(一轮基数排序)。您可以将'rd_list_append'视为追加列表'a'的函数的指针。
您也可以直接在循环中执行a.append(y)。