0

这是我现在拥有的代码。我正在尝试对从文件中读取的数字列表应用基数排序,并将排序后的列表写入不同的文件。

from math import log

b = open("radix.in.txt").readlines()
a = [int(i) for i in b]

f = open("radix.out.txt", "w")
def getIndex(num, base, digit_num):
    return (num // base ** digit_num) % base  

def newLists(size):
    return [ [] for i in range(size) ]  

def order(a, base, digit_num):
    placehold = newLists(base)
    for num in a:
        placehold[getIndex(num, base, digit_num)].append(num)  
    return placehold

def radixSort(a, base):
    passes = 3
    new_list = a
    for digit_num in range(passes):
        new_list = order(new_list, base, digit_num)
    list_c = [str(i) for i in new_list]
    print list_c

radixSort(a, 10)

输入文件只是要排序的所有数字的列表,每行一个数字

追溯:

File "C:\Users\Nolan Caldwell\Desktop\HW5.py", line 23, in radixSort new_list = order(new_list, base, digit_num) 
File "C:\Users\Nolan Caldwell\Desktop\HW5.py", line 16, in order placehold[getIndex(num, base, digit_num)].append(num) 
File "C:\Users\Nolan Caldwell\Desktop\HW5.py", line 8, in getIndex return (num // base ** digit_num) % base 
TypeError: unsupported operand type(s) for //: 'list' and 'int'
4

1 回答 1

4

您的函数order()需要一个整数列表作为第一个参数a,这就是第一次迭代中传递的内容。

但是,order()返回一个列表列表,并且该新列表被传递而不是a在第二次迭代中传递radixSort(),这就是它失败的原因。

于 2013-04-16T05:21:30.207 回答