如何清除这个AttributeError "nonetype"?
How to clear this AttributeError "nonetype"?
我在编译这个归并排序链表时正在学习数据结构。我收到了这个错误。我尝试了很多但没有奏效。谁能告诉我怎么了?请.
回溯(最后一次调用):
文件“C:\Users**\AppData\Local\Programs\Python\Python37\merge_sort_ver_2.0 链接 list.py”,第 122 行,在
中
sorted_list=merge_sort(lst)
文件“C:\Users**\AppData\Local\Programs\Python\Python37\merge_sort_ver_2.0 链接 list.py”,第 75 行,在 merge_sort
中
return merge(left,right)
文件“C:\Users**\AppData\Local\Programs\Python\Python37\merge_sort_ver_2.0 已链接 list.py”,第 94 行,合并
left_head=left.head
AttributeError: 'NoneType' 对象没有属性 'head'
def merge_sort(ll):
if ll.length()==1:
return ll
elif ll.head is None:
return ll
left_half,right_half=split(ll)
left=merge_sort(left_half)
right=merge_sort(right_half)
return merge(left,right)
def split(ll):
if ll.head==None or ll==None:
left_half=ll
right_half=None
return left_half,right_half
else:
size=ll.length()
mid=size//2
mid_node=ll.nodeAt(mid-1)
right_half=ll
left_half=LinkedList()
left_half.head=mid_node.next
mid_node.next=None
return left_half,right_half
def merge(left,right):
merged=LinkedList()
merged.tailinsert(0)
current=merged.head
left_head=left.head
right_head=right.head
while left_head or right_head:
if left_head is None:
current.next=right_head
right_head=right_head.next
elif right_head is None:
current.next=left_head
left_head=left_head.next
else:
merged.head=head
return merged.printlist()
lst=LinkedList()
lst.tailinsert(14)
lst.tailinsert(46)
lst.tailinsert(43)
lst.tailinsert(27)
sorted_list=merge_sort(lst)
print(sorted_list)
def merge_sort(ll):
if ll.length()==1:
return ll
elif ll.head is None:
return ll # Here, you return None
left_half,right_half=split(ll)
left=merge_sort(left_half) # Then by recursion, here left=None
right=merge_sort(right_half)
return merge(left,right) # Then you inject it in merge
def split(ll):
if ll.head==None or ll==None:
left_half=ll
right_half=None
return left_half,right_half
else:
size=ll.length()
mid=size//2
mid_node=ll.nodeAt(mid-1)
right_half=ll
left_half=LinkedList()
left_half.head=mid_node.next
mid_node.next=None
return left_half,right_half
def merge(left,right): # Then here, left is None
merged=LinkedList()
merged.tailinsert(0)
current=merged.head
left_head=left.head # Then boom because None.head
right_head=right.head
现在,介绍一些方法。
遇到此类错误时,请使用print
显示函数的参数。这将对你有很大帮助。
def merge(left,right):
print(f"merge({left},{right})")
此外,在发布此类问题时,请包括带有确切错误的回溯。它会帮助这里的其他人帮助你。
感谢 Jérômei 的提示我清除了错误
def merge_sort(ll):
if ll.length()==1:
return ll
elif ll.head is None:
return ll
left_half,right_half=split(ll)
left=merge_sort(left_half)
right=merge_sort(right_half)
return merge(left,right)
def split(ll):
if ll.head==None or ll==None:
left_half=ll
right_half=None
return left_half,right_half
else:
size=ll.length()
mid=size//2
mid_node=ll.nodeAt(mid-1)
right_half=LinkedList()
left_half=ll
right_half.head=mid_node.next
mid_node.next=None
return left_half,right_half
def merge(left,right):
merged=LinkedList()
merged.tailinsert(0)
current=merged.head
left_head=left.head
right_head=right.head
while left_head and right_head :
left_data=left_head.data
right_data=right_head.data
if left_data < right_data:
current.next=left_head
left_head=left_head.next
elif right_data <left_data:
current.next=right_head
right_head=right_head.next
current=current.next
while right_head is not None :
current.next=right_head
right_head=right_head.next
current=current.next
while left_head is not None :
current.next=left_head
left_head=left_head.next
current=current.next
current=current.next
head=merged.head.next
merged.head=head
print("merged 1:")
merged.printlist()
return merged
lst=LinkedList()
lst.tailinsert(14)
lst.tailinsert(46)
lst.tailinsert(43)
lst.tailinsert(27)
merge_sort(lst)
**output:
merged 1:
14
46
merged 1:
27
43
merged 1:
14
27
43
46**
我在编译这个归并排序链表时正在学习数据结构。我收到了这个错误。我尝试了很多但没有奏效。谁能告诉我怎么了?请.
回溯(最后一次调用): 文件“C:\Users**\AppData\Local\Programs\Python\Python37\merge_sort_ver_2.0 链接 list.py”,第 122 行,在
中 sorted_list=merge_sort(lst)
文件“C:\Users**\AppData\Local\Programs\Python\Python37\merge_sort_ver_2.0 链接 list.py”,第 75 行,在 merge_sort
中return merge(left,right)
文件“C:\Users**\AppData\Local\Programs\Python\Python37\merge_sort_ver_2.0 已链接 list.py”,第 94 行,合并
left_head=left.head
AttributeError: 'NoneType' 对象没有属性 'head'
def merge_sort(ll):
if ll.length()==1:
return ll
elif ll.head is None:
return ll
left_half,right_half=split(ll)
left=merge_sort(left_half)
right=merge_sort(right_half)
return merge(left,right)
def split(ll):
if ll.head==None or ll==None:
left_half=ll
right_half=None
return left_half,right_half
else:
size=ll.length()
mid=size//2
mid_node=ll.nodeAt(mid-1)
right_half=ll
left_half=LinkedList()
left_half.head=mid_node.next
mid_node.next=None
return left_half,right_half
def merge(left,right):
merged=LinkedList()
merged.tailinsert(0)
current=merged.head
left_head=left.head
right_head=right.head
while left_head or right_head:
if left_head is None:
current.next=right_head
right_head=right_head.next
elif right_head is None:
current.next=left_head
left_head=left_head.next
else:
merged.head=head
return merged.printlist()
lst=LinkedList()
lst.tailinsert(14)
lst.tailinsert(46)
lst.tailinsert(43)
lst.tailinsert(27)
sorted_list=merge_sort(lst)
print(sorted_list)
def merge_sort(ll):
if ll.length()==1:
return ll
elif ll.head is None:
return ll # Here, you return None
left_half,right_half=split(ll)
left=merge_sort(left_half) # Then by recursion, here left=None
right=merge_sort(right_half)
return merge(left,right) # Then you inject it in merge
def split(ll):
if ll.head==None or ll==None:
left_half=ll
right_half=None
return left_half,right_half
else:
size=ll.length()
mid=size//2
mid_node=ll.nodeAt(mid-1)
right_half=ll
left_half=LinkedList()
left_half.head=mid_node.next
mid_node.next=None
return left_half,right_half
def merge(left,right): # Then here, left is None
merged=LinkedList()
merged.tailinsert(0)
current=merged.head
left_head=left.head # Then boom because None.head
right_head=right.head
现在,介绍一些方法。
遇到此类错误时,请使用print
显示函数的参数。这将对你有很大帮助。
def merge(left,right):
print(f"merge({left},{right})")
此外,在发布此类问题时,请包括带有确切错误的回溯。它会帮助这里的其他人帮助你。
感谢 Jérômei 的提示我清除了错误
def merge_sort(ll):
if ll.length()==1:
return ll
elif ll.head is None:
return ll
left_half,right_half=split(ll)
left=merge_sort(left_half)
right=merge_sort(right_half)
return merge(left,right)
def split(ll):
if ll.head==None or ll==None:
left_half=ll
right_half=None
return left_half,right_half
else:
size=ll.length()
mid=size//2
mid_node=ll.nodeAt(mid-1)
right_half=LinkedList()
left_half=ll
right_half.head=mid_node.next
mid_node.next=None
return left_half,right_half
def merge(left,right):
merged=LinkedList()
merged.tailinsert(0)
current=merged.head
left_head=left.head
right_head=right.head
while left_head and right_head :
left_data=left_head.data
right_data=right_head.data
if left_data < right_data:
current.next=left_head
left_head=left_head.next
elif right_data <left_data:
current.next=right_head
right_head=right_head.next
current=current.next
while right_head is not None :
current.next=right_head
right_head=right_head.next
current=current.next
while left_head is not None :
current.next=left_head
left_head=left_head.next
current=current.next
current=current.next
head=merged.head.next
merged.head=head
print("merged 1:")
merged.printlist()
return merged
lst=LinkedList()
lst.tailinsert(14)
lst.tailinsert(46)
lst.tailinsert(43)
lst.tailinsert(27)
merge_sort(lst)
**output:
merged 1:
14
46
merged 1:
27
43
merged 1:
14
27
43
46**