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