我的代碼是這樣的。我正在嘗試將列表中的數字排序到列表列表中,然後將列表列表寫入文件,並將每個列表放在不同的行上。如果我嘗試使用f.write
,它會給我一個字符緩衝區的預期錯誤。如何在python中將列表列表寫入文件?
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)
把f.write(list_c)在打印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
您可以發佈完整的錯誤? – thegrinner