我试图实现我自己的基数排序版本。我已注释掉的程序底部的大部分按预期工作而没有错误,但是,当我将它放入方法调用main_sort时,我收到一个错误:' Nontype'对象不可迭代。它实际上是相同的代码,复制和粘贴,我看不出我做错了什么。我已经指出了发生错误的行,但正如我所说,我不知道为什么我会得到错误。
def read_file(file):
with open(file) as f:
content = [x.strip('\n') for x in f.readlines()]
f.close()
return content
def create_bins(list):
if list == listDec:
sortBins = {item : [] for item in dictionIndexes[:10]}
return sortBins
elif list == listOct:
sortBins = {item : [] for item in dictionIndexes[:8]}
return sortBins
elif list == listHex:
sortBins = {item : [] for item in dictionIndexes}
return sortBins
def sort(list, index):
bins = create_bins(list)
for key in bins: //WHERE THE ERROR OCCURS
for num in list:
if num[index] == key:
bins[key].append(num)
return bins
def main_sort(list):
mainBin = []
index = -1
while index > -11:
sortBins = sort(list, index)
for key in sorted(sortBins):
for items in sortBins[key]:
mainBin.append(items)
if index == -10:
break
else:
list = mainBin
mainBin = []
index = index - 1
return list
# mainBin = []
dictionIndexes = "0123456789ABCDEF"
sortBins = {}
#Number lists
listDec = read_file("Number Lists/random_numbers10.txt")
listHex = read_file("Number Lists/random_numbers4.txt")
listOct = read_file("Number Lists/random_numbers3.txt")
# outputFile = open("output.txt", "w")
#The main sort part, just need to change between listHex, Oct and Dec
# index = -1
# while index > -11:
# sortBins = sort(listHex, index)
# for key in sorted(sortBins):
# for items in sortBins[key]:
# mainBin.append(items)
# if index == -10:
# break
# else:
# listHex = mainBin
# mainBin = []
# index = index - 1
# for item in mainBin:
# outputFile.write(item + "\n")
var = main_sort(listDec)
答案 0 :(得分:0)
您的问题是,在main_sort
内,您执行了list=mainBin
,因此list
在迭代后会指向除ListDec
之外的其他内容。< / p>
我添加了一些打印件,以便您了解问题所在:
def create_bins(list):
if list == listDec:
print("ListDec")
sortBins = {item : [] for item in dictionIndexes[:10]}
return sortBins
elif list == listOct:
sortBins = {item : [] for item in dictionIndexes[:8]}
return sortBins
elif list == listHex:
sortBins = {item : [] for item in dictionIndexes}
return sortBins
else:
print("not in the list ...")
def sort(list, index):
print(list)
bins = create_bins(list)
print(bins)
for key in bins: #WHERE THE ERROR OCCURS
for num in list:
if num[index] == key:
bins[key].append(num)
return bins
def main_sort(list):
mainBin = []
index = -1
while index > -11:
sortBins = sort(list, index)
for key in sorted(sortBins):
for items in sortBins[key]:
mainBin.append(items)
if index == -10:
break
else:
list = mainBin
mainBin = []
index = index - 1
return list
结果:
# first iteration
['22', '12', '472', '224', '15653', '545']
ListDec
{'3': [], '4': [], '5': [], '9': [], '0': [], '2': [], '7': [], '1': [], '8': [], '6': []}
# second iteration ... List != ListDec
['22', '12', '472', '15653', '224', '545']
not in the list ...
None
Traceback (most recent call last):
File "sort.py", line 73, in <module>
var = main_sort(listDec)
File "sort.py", line 35, in main_sort
sortBins = sort(list, index)
File "sort.py", line 25, in sort
for key in bins: #WHERE THE ERROR OCCURS
TypeError: 'NoneType' object is not iterable
怎么办?
我建议重新处理create_bins
函数,这样就不会通过测试当前列表的相等性来检查数据类型(Dec / Hex / Oct)