我们如何修改传递给Python函数的对象

发布于 2025-01-30 11:18:28 字数 1375 浏览 4 评论 0原文

我创建了一个小函数function,如果传递-1的值,则应将none分配给节点,然后将值分配给节点对象的值属性。我创建了一个简单的二进制树[根,左,右],最后打印了节点。

class TreeNode:
    def __init__(self, val=0, left=None, right=None):
        self.val = val
        self.left = left
        self.right = right

def funct(node, val):
    if val == -1:
        ### modify code here 
        node = None
    else:
        node.val = val

root = TreeNode()
root.val = 'root'
root.left = TreeNode()
root.right = TreeNode()

funct(root.left, 'left')
funct(root.left, -1)

print(root, root.val)
print(root.left, root.left.val)
print(root.right, root.right.val)

当我打印节点时,我会看到以下输出。

正确的节点在内存中,不是

如何通过修改中的代码( in function> function中)以获取以下输出,从而将分配给Orignal对象。

大量模拟以下代码并获取输出:

root = TreeNode()
root.val = 'root'
root.left = TreeNode('left')
root.right = None

“

注意:我可以更改我的算法。仅在Val!= -1时创建一个新节点。但是,我想了解是否有一种方法可以在Pyhton修改经过的对象。

编辑:删除“删除”一词。添加了一些澄清。

I created a small function funct which should assign None to node if a value of -1 is passed else assign the value to the value attribute of the node object. I created a simple binary tree [root, left, right] and finally print the nodes.

class TreeNode:
    def __init__(self, val=0, left=None, right=None):
        self.val = val
        self.left = left
        self.right = right

def funct(node, val):
    if val == -1:
        ### modify code here 
        node = None
    else:
        node.val = val

root = TreeNode()
root.val = 'root'
root.left = TreeNode()
root.right = TreeNode()

funct(root.left, 'left')
funct(root.left, -1)

print(root, root.val)
print(root.left, root.left.val)
print(root.right, root.right.val)

When I print the nodes I see the following output.

output

The right node is in memory and is not None.

How do I assign None to the orignal object by modifying the code inside the if in funct to get the following output instead.

esentially simulating the following code and getting the output :

root = TreeNode()
root.val = 'root'
root.left = TreeNode('left')
root.right = None

output2

Note : I can change my algo. to create a new node only when val != -1. However I want to understand if there is a way to modify a passed object it in pyhton.

Edits : removed the word "delete". Added some clarification.

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

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

发布评论

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

评论(2

放赐 2025-02-06 11:18:28

i创建了一个小函数,如果传递-1的值,该函数应分配给节点,否则将值分配给节点对象的值属性。

funct无法替换传递给它的节点(即:使呼叫者的变量之一是指新节点,而不是该节点),因为它接收 node ,和不是该节点的任何变量名称。

它可以修改传递给它的节点。特别是,它可以通过重新分配到node.rightnode.left.left来替换子值。这意味着“ .right.lefttreenode实例传递给我”。 node是一个名称function用于该传递实例的名称,与 无关的是呼叫代码

那么,要“删除”子树,我们需要通过将要删除的树的 parent parent,并指示将删除哪一侧。看起来可能是:

def remove_child(parent, which):
    if which == 'left':
        parent.left = None
    elif which == 'right':
        parent.right = None
    else:
        raise ValueError("invalid child name")

树的根没有父母,因此除去整棵树必须被视为特殊情况。

I created a small function funct which should assign None to node if a value of -1 is passed else assign the value to the value attribute of the node object.

funct cannot replace the node that was passed to it (i.e.: make one of the caller's variables refer to a new node instead of this one), because it receives the node, and not any variable name for that node.

It can modify the node that was passed to it. In particular, it can replace the child values, by reassigning to node.right and node.left. This means "the .right and .left of the TreeNode instance that was passed to me". node is a name that funct uses for that passed-in instance, that has nothing to do with the calling code in any way.

To "remove" a subtree, then, we need to pass the parent of the tree that will be removed, and also indicate which side will be removed. That might look like:

def remove_child(parent, which):
    if which == 'left':
        parent.left = None
    elif which == 'right':
        parent.right = None
    else:
        raise ValueError("invalid child name")

The root of the tree has no parent, so removing the entire tree must be treated as a special case.

爱她像谁 2025-02-06 11:18:28

不确定这是否是您需要的,但我认为它可以做您想要的行为:

class TreeNode:
    def __init__(self, val=0, left=None, right=None):
        self.val = val
        self.left = left
        self.right = right

    def funct(self, child: str, val):
        if val == -1:
        ### modify code here 
            setattr(self,child, None)
    
        else:
            setattr(self,child,TreeNode(val))


root = TreeNode()
root.val = 'root'
root.left = TreeNode()
root.right = TreeNode()

root.funct(child='left', val='left')
root.funct(child='right', val='right')

print(root, root.val)
print(root.left, root.left.val)
print(root.right, root.right.val)
print(None)

root.funct(child='left', val= -1)
print("\n")
print(root, root.val)
# print(root.left, root.left.val) - you cannot print root.left.val because None has no attribute
print(root.left)
print(root.right, root.right.val)

Not sure if this is what you need, but I think its doing the behavior you want:

class TreeNode:
    def __init__(self, val=0, left=None, right=None):
        self.val = val
        self.left = left
        self.right = right

    def funct(self, child: str, val):
        if val == -1:
        ### modify code here 
            setattr(self,child, None)
    
        else:
            setattr(self,child,TreeNode(val))


root = TreeNode()
root.val = 'root'
root.left = TreeNode()
root.right = TreeNode()

root.funct(child='left', val='left')
root.funct(child='right', val='right')

print(root, root.val)
print(root.left, root.left.val)
print(root.right, root.right.val)
print(None)

root.funct(child='left', val= -1)
print("\n")
print(root, root.val)
# print(root.left, root.left.val) - you cannot print root.left.val because None has no attribute
print(root.left)
print(root.right, root.right.val)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文