我们如何修改传递给Python函数的对象
我创建了一个小函数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.
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
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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
funct
无法替换传递给它的节点(即:使呼叫者的变量之一是指新节点,而不是该节点),因为它接收 node ,和不是该节点的任何变量名称。它可以修改传递给它的节点。特别是,它可以通过重新分配到
node.right
和node.left.left
来替换子值。这意味着“.right
和.left
的treenode
实例传递给我”。node
是一个名称function
用于该传递实例的名称,与 无关的是呼叫代码 。那么,要“删除”子树,我们需要通过将要删除的树的 parent parent,并指示将删除哪一侧。看起来可能是:
树的根没有父母,因此除去整棵树必须被视为特殊情况。
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
andnode.left
. This means "the.right
and.left
of theTreeNode
instance that was passed to me".node
is a name thatfunct
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:
The root of the tree has no parent, so removing the entire tree must be treated as a special case.
不确定这是否是您需要的,但我认为它可以做您想要的行为:
Not sure if this is what you need, but I think its doing the behavior you want: