gpt4 book ai didi

python - 无法合并两个已排序的单链表,因为 "type object ' _Node' 没有属性 '_element' "

转载 作者:行者123 更新时间:2023-11-28 18:58:28 24 4
gpt4 key购买 nike

我正在制作自己的 merge_list 函数。我已经创建了类 SList() 和类 _Node()。但是当我在 merge_list 中输入两个排序列表时,merge_list 函数无法比较两个节点的值,显示 type object '_Node' has no attribute '_element', but class _Node have _element attribute(I also try元素,但它也不起作用)。我的函数有什么问题??

这是我的 class SList()class _Node() 的部分代码以及 merge_list 的完整代码

class SList:
class _Node:
def __init__(self, element, next=None):
self._element = element
self._next = next

def element(self):
return self._element

def next(self):
return self._next

def set_element(self, element):
self._element = element

def set_next(self, next):
self._next = next

def __init__(self, head=None):
self._head = head

def insert_first(self, element):
self._head = self._Node(element, self._head)

def insert_after(self, element, p):
new_node = self._Node(element, p.next())
p._next = new_node



...和我的 merge_list 函数

def merge_list(s1, s2):
s3 = SList()
s3._Node(None, None)
prev = s3

while s1 != None and s2 != None:
if s1._Node._element<= s2._Node._element: # this line is the problem
prev.next = s1
s1 = s1.next
else:
prev.next = s2
s2 = s2.next
prev = prev.next

if s1 == None:
prev.next = s2
elif s2 == None:
prev.next = s1

return s3.next

我做了这个函数之后,我输入了两个这样的排序列表并调用了merge_list函数

if __name__ == "__main__":
s1 = SList()
s1.insert_first(1)
s1.insert_first(3)
s1.insert_first(5)
s1.insert_first(7)
s1.insert_first(9)
s1.insert_first(11)
s1.insert_first(13)
print(f"s1 :", s1)


s2 = SList()
s2.insert_first(2)
s2.insert_first(4)
s2.insert_first(6)
print(f"s2 : ", s2)
merge_list(s1, s2)

输出是

Traceback (most recent call last):
s1 : 13 -> 11 -> 9 -> 7 -> 5 -> 3 -> 1 -> None: 7 element(s)
s2 : 6 -> 4 -> 2 -> None: 3 element(s)
File "", line 334, in <module>
merge_list(s1, s2)
File "", line 151, in merge_list
if s1._Node._element<= s2._Node._element:
AttributeError: type object '_Node' has no attribute '_element'

似乎s1、s2列表创建成功,但是无法比较节点的值。 merge_list 函数有什么问题??

最佳答案

让我们一步一步来

代码分析:

class SList:
class _Node:

注意 为什么要在 SList 类中定义 Node 类?这不是常见的做法,除非您永远不希望从 Slist 外部访问节点(即使那样)

        def __init__(self, element, next=None):
self._element = element
self._next = next

def element(self):
return self._element

def next(self):
return self._next

def set_element(self, element):
self._element = element

def set_next(self, next):
self._next = next

注意 Init/getters 和 setters。到现在为止,一切都说得通了。但是,我个人更喜欢库@attrs。但我们稍后会谈到这一点。

    def __init__(self, head=None):
self._head = head

def insert_first(self, element):
self._head = self._Node(element, self._head)

def insert_after(self, element, p):
new_node = self._Node(element, p.next())
p._next = new_node


def merge_list(s1, s2):
s3 = SList()
s3._Node(None, None)
prev = s3

while s1 != None and s2 != None:
if s1._Node._element<= s2._Node._element: # this line is the problem

好吧,由于评论中提到的原因,这根本行不通:类 _Node 没有 _element 属性。实例确实如此。

            prev.next = s1

prevs3,它是 SList,因此 next 不应该有属性?

            s1 = s1.next
else:
prev.next = s2
s2 = s2.next
prev = prev.next

if s1 == None:
prev.next = s2
elif s2 == None:
prev.next = s1

return s3.next

首先尝试提供帮助(注意:这是一个基于 OP 提供的工作解决方案,而不是尝试编写干净的代码。)

class _Node:
def __init__(self, element, next=None):
self._element = element
self._next = next

@property
def element(self):
return self._element
@property
def next(self):
return self._next

def set_element(self, element):
self._element = element

def set_next(self, next):
self._next = next

class SList:
def __init__(self, head=None):
self._head = _Node(head)

@property
def head(self):
return self._head

def insert_first(self, element):
self._head = _Node(element, self._head)

def insert_after(self, element, p):
new_node = _Node(element, p.next())
p._next = new_node

def merge_list(s1, s2):
s3 = SList()
prev = s3._head
head_s1 = s1._head
head_s2 = s2._head

while (head_s1._element != None) and (head_s2._element != None):
if head_s1._element<= head_s2._element: # this line is the problem
prev.set_next(head_s1)
head_s1 = head_s1.next
else:
prev.set_next(head_s2)
head_s2 = head_s2.next
prev = prev.next

if head_s1 == None:
prev.set_next(head_s1)
elif head_s2 == None:
prev.set_next(head_s2)

return s3

关于python - 无法合并两个已排序的单链表,因为 "type object ' _Node' 没有属性 '_element' ",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55591163/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com