我的代码是这样的。我正在尝试将数字列表进行基数排序到列表列表中,然后将该列表列表写入文件,每个单独的列表位于不同的行上。如果我尝试使用f.write
,它会给我一个字符缓冲区预期的错误。
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(base):
return [ [] for i in range(base) ]
def order(q, 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)
在打印 list_c 之后放入 f.write(list_c) 会给出这个回溯
Traceback (most recent call last):
File "C:\Users\Nolan Caldwell\Desktop\HW5.py", line 29, in <module>
radixSort(a, 10)
File "C:\Users\Nolan Caldwell\Desktop\HW5.py", line 26, in radixSort
f.write(list_c)
TypeError: expected a character buffer object