Python:函数来改变'就地'的值? [英] Python: functions to change values 'in place'?

查看:180
本文介绍了Python:函数来改变'就地'的值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想实现一个函数,允许其参数值重新分配。



作为一个例子,一个函数会增加参数 x 和递减参数 y 。 (这只是一个简单的示例 - 其动机是 X Y 实际上是大数据框中的单个元素;它们的表达式很难使用;而此操作将)

  def incdec(x,y,d):
x + = d
y - = d

理想情况下,这将运行为:

  X = 5; Y = 7; d = 2 
incdec(X,Y,d)

找到这些值现在 X = 7和 Y = 5。但当然它不会这样工作 - 我想知道为什么? h2_lin>解决方案

为什么你的函数不改变X和Y的最终值?

在Python中当一个带参数的函数是所谓的
参数值的副本存储在局部变量中。
事实上,当你写出

$ $ p $ def incdec(x,y,d)时:
x + = d
y - = d

唯一改变的是函数IN中的x和y INDEC。
但是在函数结束时,局部变量丢失了。
为了获得你想要的东西,你应该记住函数做了什么。
要记住函数后的那些值,您应该重新指定x和y,如下所示:

  def incdec(x ,y,d):
x + = d
y - = d
return(x,y)

#然后

X = 5; Y = 7; d = 2
X,Y = incdec(X,Y,d)

因为X,Y是int类型的。
您也可以使用列表直接访问您想要更改的变量。

  def incdec(list_1,d):
list_1 [0] + = d
list_1 [1] - = d
#不需要返回

#然后
X = 5; Y = 7; d = 2
new_list = [X,Y]
incdec(new_list,d)#列表已更改,但不包含X和Y

不要误会我的意思,我之前说过的参数仍然是一个副本,但是当您复制一个列表时,只会复制引用,但它们仍然在看同样的目的。下面是一个演示:

  number = 5 
list_a = [number] #we复制数字$ b $的值b print(list_a [0])#output is 5
list_b = list_a#我们将所有引用os list_a复制到list_b
print(list_b [0])中#输出为5
list_a [0 ] = 99
print(list_b [0])#output is 99
print(number)#output is 5

你可以看到 list_a [0]和list_b [0] 是同一个对象,但是数字是不同的
因为我们复制了 number 的值而不是引用。
我建议您使用第一种解决方案。
我希望这有助于。


I'd like to implement a function that allows the values of its arguments to be reallocated 'in place'.

As an example, a function that will increment argument x and decrement argument y. (This is just a simple example for illustration - the motivation is that X and Y are in fact single elements of a large dataframe; their expressions are unwieldy; and this operation will undergo many iterations.)

def incdec(x,y,d):
    x += d
    y -= d

Ideally this would be run as:

X = 5; Y = 7; d = 2
incdec(X,Y,d)

to find that the values are now X = 7 and Y = 5. But of course it doesn't work like that - I wondered why?

解决方案

Why does your function not change the final values of X and Y ?

In Python When a function with arguments is called, copies of the values of the arguments are stored in local variables. Indeed when you write

def incdec(x,y,d):
    x += d
    y -= d

the only thing that changes are the x and y that are IN THE function indec. But at the end of the function local variables are lost. To obtain what you want you should remember what the function did. To remember those values AFTER the function you should re-assigne x and y like this:

def incdec(x,y,d):
    x += d
    y -= d
    return (x,y)

# and then 

X = 5; Y = 7; d = 2
X,Y = incdec(X,Y,d)

this works because X,Y are of type int. What you also could do is using a list to have a direct access to the variables you want to change.

def incdec(list_1,d):
    list_1[0] += d
    list_1[1] -= d
    #no return needed

# and then 
X = 5; Y = 7; d = 2
new_list = [X, Y]
incdec(new_list,d) #the list has changed but not X and Y

Don t get me wrong, the arguments passed are still a copy as I said earlier but when you copy a list, only references are copied, but those are still looking at the same object. Here's a demo:

number = 5
list_a = [number] #we copy the value of number
print (list_a[0]) # output is 5
list_b = list_a # we copy all references os list_a into list_b
print(list_b[0]) # output is 5
list_a[0]=99
print(list_b[0]) # output is 99
print(number)    # output is 5

as you can see list_a[0] and list_b[0] is one same object but number is a different one That s because we copied the value of number and not the reference. I recommend you to use the first solution. I hope this helped.

这篇关于Python:函数来改变'就地'的值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆