尽管我已经定义了函数,但代码有什么问题?
What's wrong with the code although i have defined the function already?
这段代码给出了 NameError 显示我没有定义函数但我实际上在 class.
中定义了函数
我试了很多都解决不了
class Node(object):
def __init__(self, data):
self.data = data
self.next = None
class LinkedList(object):
def __init__(self):
self.head = None
def printList(self):
temp = self.head
while temp:
print(temp.data)
temp = temp.next
def append(self, new_data):
new_node = Node(new_data)
if self.head is None:
self.head = new_node
return
last = self.head
while last.next:
last = last.next
last.next = new_node
def merge(self, head1, head2):
temp = None
if head1 is None:
return head2
if head2 is None:
return head1
if head1.data <= head2.data:
temp = head1
temp.next = merge(head1.next, head2)
else:
temp = head2
temp.next = merge(head1, head2.next)
return temp
list1 = LinkedList()
list1.append(10)
list1.append(20)
list1.append(30)
list1.append(40)
list1.append(50)
list2 = LinkedList()
list2.append(5)
list2.append(15)
list2.append(18)
list2.append(35)
list2.append(60)
list3 = LinkedList()
list3.head = merge(list1.head, list2.head)
print("Merged Linked List is : ")
list3.printList()
NameError Traceback (most recent call last)
<ipython-input-11-22fca0a2d24d> in <module>()
57
58 list3 = LinkedList()
---> 59 list3.head = merge(list1.head, list2.head)
60
61 print("Merged Linked List is : ")
NameError: name 'merge' is not defined
您已将 merge
定义为 LinkedList
class 的方法。这意味着您需要在 class 的实例上调用它,而不仅仅是它本身。例如,您可以将导致当前异常的 merge(...)
调用替换为 list3.merge(...)
,并将方法内部的递归调用替换为 self.merge(...)
.
但我不太确定它是否需要是一个方法,因此您可以将函数定义移出 class(并删除其 self
参数)。它可以作为 stand-alone 函数正常工作。
这段代码给出了 NameError 显示我没有定义函数但我实际上在 class.
中定义了函数我试了很多都解决不了
class Node(object):
def __init__(self, data):
self.data = data
self.next = None
class LinkedList(object):
def __init__(self):
self.head = None
def printList(self):
temp = self.head
while temp:
print(temp.data)
temp = temp.next
def append(self, new_data):
new_node = Node(new_data)
if self.head is None:
self.head = new_node
return
last = self.head
while last.next:
last = last.next
last.next = new_node
def merge(self, head1, head2):
temp = None
if head1 is None:
return head2
if head2 is None:
return head1
if head1.data <= head2.data:
temp = head1
temp.next = merge(head1.next, head2)
else:
temp = head2
temp.next = merge(head1, head2.next)
return temp
list1 = LinkedList()
list1.append(10)
list1.append(20)
list1.append(30)
list1.append(40)
list1.append(50)
list2 = LinkedList()
list2.append(5)
list2.append(15)
list2.append(18)
list2.append(35)
list2.append(60)
list3 = LinkedList()
list3.head = merge(list1.head, list2.head)
print("Merged Linked List is : ")
list3.printList()
NameError Traceback (most recent call last)
<ipython-input-11-22fca0a2d24d> in <module>()
57
58 list3 = LinkedList()
---> 59 list3.head = merge(list1.head, list2.head)
60
61 print("Merged Linked List is : ")
NameError: name 'merge' is not defined
您已将 merge
定义为 LinkedList
class 的方法。这意味着您需要在 class 的实例上调用它,而不仅仅是它本身。例如,您可以将导致当前异常的 merge(...)
调用替换为 list3.merge(...)
,并将方法内部的递归调用替换为 self.merge(...)
.
但我不太确定它是否需要是一个方法,因此您可以将函数定义移出 class(并删除其 self
参数)。它可以作为 stand-alone 函数正常工作。