异常记录 RecursionError: maximum recursion depth exceeded in comparison
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
num = [1, 2, 3] label = ['A', 'B']
defcreateTree(): #print(label) if len(label) == 0: return data = {label[0]: {}} for i in range(len(num)): if i == 2: data[label.pop(0)][i] = createTree() else: data[label[0]][i] = 'YES' return data
defcreateTree(): if len(label) == 0: return data = {label[0]: {}} for i in range(len(num)): if i == 2: best = label.pop(0) data[best][i] = createTree() else: data[label[0]][i] = 'YES' return data
print(createTree())
答案和成品的答案是一样的,所以也验证了我之前的猜想。
最后的成品 1
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
num = [1, 2, 3] label = ['A', 'B']
defcreateTree(): if len(label) != 0: #当标签里面还有元素的时候 best = label[0] #创建一个新的字典 data = {best: {}} if len(label) == 0: #如果 label 的长度为零,则说明标签已经没有元素了 return del (label[0]) for i in range(len(num)): if i == 2: data[best][i] = createTree() else: data[best][i] = 'YES' return data
defcreateTree(): if len(label) != 0: #当标签里面还有元素的时候 best = label[0] #创建一个新的字典 data = {best: {}} if len(label) == 0: #如果 label 的长度为零,则说明标签已经没有元素了 return label.pop(0) for i in range(len(num)): if i == 2: data[best][i] = createTree() else: data[best][i] = 'YES' return data
defcreateTree(label): if len(label) != 0: best = label[0] data = {best: {}} if len(label) == 0: return del (label[0]) for i in range(len(num)): if i == 2: data[best][i] = createTree(change(label)) else: data[best][i] = 'YES' return data
defcreateTree(): if len(label) == 0: return best = label[0] data = {best: {}} label.pop(0) for i in range(len(num)): if i == 2: data[best][i] = createTree() else: data[best][i] = 'YES' return data