修改函数中的类以反映呼叫功能的变化(Python)

发布于 2025-02-13 21:26:19 字数 836 浏览 0 评论 0原文

我的类如下:

class ListNode:
     def __init__(self, val=0, next=None):
         self.val = val
         self.next = next

我创建一个链接列表为:

ll = ListNode(1)
ll.next = ListNode(2)

我想通过函数中的创建新节点将列表复制到新列表:

def create_new_node(res, l):
            print(l.val)
            res.next = ListNode(l.val)
            l=l.next
            print("Next", l.val)
            res=res.next

res = ListNode()
while ll:
        # Based on some condition
        create_new_node(res, ll)

>>> 1
Next 2
1
Next 2
....

但是,由于ll,这将变成无限循环。在create_node_function之外,切勿在while循环中进行下一个节点,即使l(这是ll)的引用)要指向next节点为l = l.next

是否不在Python中的参考中通过类,并且不能在另一个函数中修改它们以反映调用函数的更改吗?

I have a class as follows:

class ListNode:
     def __init__(self, val=0, next=None):
         self.val = val
         self.next = next

I create a linked list as:

ll = ListNode(1)
ll.next = ListNode(2)

I want to copy the list to a new list by create new nodes in a function:

def create_new_node(res, l):
            print(l.val)
            res.next = ListNode(l.val)
            l=l.next
            print("Next", l.val)
            res=res.next

res = ListNode()
while ll:
        # Based on some condition
        create_new_node(res, ll)

>>> 1
Next 2
1
Next 2
....

However, this goes into a infinite loop since the ll never progresses to the next node in the while loop outside of the create_node_function even though l (which is a reference of ll) is being made to point to the next node as l=l.next.

Are classes not passed by reference in Python, and can they not be modified in another function to reflect the changes in the calling function?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

故事灯 2025-02-20 21:26:19

在Python中,对象通过分配传递。您可以使用以下代码看到此信息。

test_list = [1,2,3]
test_dict = {1:'a', 2:'b'}
test_tup = (1,2,3)
test_int = 5
test_str = 'hello world'

def change_test(mylist, mydict,myint, mystr):
    print('list: ', id(mylist), 'dict: ', id(mydict),'int: ', id(myint),'str: ', id(mystr))


change_test(test_list, test_dict, test_int, test_str)

print('list: ', id(test_list),'dict: ', id(test_dict),'int: ', id(test_int),'str: ', id(test_str))

输出是:

list:  4346425984 dict:  4347616128 int:  4345676144 str:  4347344688
list:  4346425984 dict:  4347616128 int:  4345676144 str:  4347344688

您会看到函数内部和外部ID相同,因此它们的值相同。但是,当您更改可变对象时,ID的保持不变,并且使用不变的对象ID会更改。

课堂实例是可变的,因此它们的作用与参考的通过相似。我们可以通过在以下代码中检查每个对象上的ID,可以看到这一点。

def create_new_node(res, l):
    print('in func: ', id(l))
    l.val = 99

ll = ListNode(1)
ll.next = ListNode(2)

res = ListNode()
print('out of func: ', id(ll), ll.val)
create_new_node(res, ll)
print('out of func: ', id(ll), ll.val) 

输出是

out of func:  4334042752 1
in func:  4334042752
out of func:  4334042752 99

您的代码的问题是

l=l.next

当我们将ll传递到create_new_node l是ll时,但是当我们将l设置为l.next时,l现在是l.next而不是ll,l.next在内存中具有不同的地址。为了使您的代码工作并复制列表返回l。

def create_new_node(res, l):
    res.next = ListNode(l.val)
    l=l.next
    res=res.next
    return l

ll = ListNode(1)
ll.next = ListNode(2)

res = ListNode(ll.val)

current = ll
while current:
    current = create_new_node(res, current)

In python objects are passed by assignment. You can see this with the following code.

test_list = [1,2,3]
test_dict = {1:'a', 2:'b'}
test_tup = (1,2,3)
test_int = 5
test_str = 'hello world'

def change_test(mylist, mydict,myint, mystr):
    print('list: ', id(mylist), 'dict: ', id(mydict),'int: ', id(myint),'str: ', id(mystr))


change_test(test_list, test_dict, test_int, test_str)

print('list: ', id(test_list),'dict: ', id(test_dict),'int: ', id(test_int),'str: ', id(test_str))

the output is:

list:  4346425984 dict:  4347616128 int:  4345676144 str:  4347344688
list:  4346425984 dict:  4347616128 int:  4345676144 str:  4347344688

You see the id's inside and outside the function are the same, so they are the same value. However, when you change a mutable object the id's stays the same and with immutable objects the id changes.

Class instances are mutable so they will act similar to pass by reference. We can see this by checking the id's on each object in the following code.

def create_new_node(res, l):
    print('in func: ', id(l))
    l.val = 99

ll = ListNode(1)
ll.next = ListNode(2)

res = ListNode()
print('out of func: ', id(ll), ll.val)
create_new_node(res, ll)
print('out of func: ', id(ll), ll.val) 

the output is

out of func:  4334042752 1
in func:  4334042752
out of func:  4334042752 99

The problem with your code is the line

l=l.next

when we pass ll into create_new_node l is ll but when we set l to l.next, l is now l.next and not ll, l.next has a different address in memory. To make your code work and copy the list return l like so.

def create_new_node(res, l):
    res.next = ListNode(l.val)
    l=l.next
    res=res.next
    return l

ll = ListNode(1)
ll.next = ListNode(2)

res = ListNode(ll.val)

current = ll
while current:
    current = create_new_node(res, current)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文