用于“按值分配”的优选方法。 [英] Preferred method for "Assignment by value"
问题描述
作为Python的一个相对较新的角色,我没有做过很多关于它的黑客攻击。我第一次尝试使用Python的古怪(至少是
我)倾向于通过引用而不是值来分配(我是
来自一个VBA世界,这就是我正在使用的术语。我感到惊讶的是,这两个案件表现得如此不同
test = [[1],[2]]
x = test [0]
x [0] = 5
test
>> [[5],[2]]
x = 1
test
>>> [[5],[ 2]]
x
>> 1
现在我做了一点阅读,我认为我理解这个问题......
我的问题是,什么是'最佳实践'的方式,只需将某物的价值分配给某个物品新名字?"
即
test = [[1,2],[3,4]]
我需要用上面的第一个列表做一些数据操作
列表没有更改< test>
显然x = test [0]将无效,因为我所做的任何更改都会改变
原始...
我发现我可以这样做:
x = [] +测试[0]
让我得到一个纯粹的 ; (即没有连接到测试[0])列表,但
关心我有点kludgy
感谢您的时间和帮助。
ha ******* @ gmail.com 写道:
作为Python的一个相对较新的人,我没有做过很多
黑客攻击与它相伴。我第一次尝试使用Python的古怪(至少是
我)倾向于通过引用而不是值来分配(我是
来自一个VBA世界,这就是我正在使用的术语。我感到惊讶的是,这两个案件表现得如此不同
test = [[1],[2]]
x = test [0]
x [0] = 5
test
>>> [[5],[2]]
x = 1
test
>>> [[5],[2]]
x
>>> 1
现在我做了一点阅读,我想我明白了这个问题...
我的问题是,什么是''最佳实践''将某事物的价值分配给新名称的方式是什么?
ie
test = [[1, 2],[3,4]]
我需要用上面的第一个列表做一些数据操作
lis t没有改变< test>
显然x = test [0]将无法正常工作,因为我所做的任何更改都会改变
原始...
我发现我可以这样做:
x = [] + test [0]
让我得到一个纯粹的 (即没有连接到测试[0])列表,但
关心我有点kludgy
感谢您的时间和帮助。
如果你想要一个对象(或其中一部分)的*副本*,你必须明确地* b $ b指定*副本。根据有问题的对象,这是不同的方式。
可以使用切片语法将列表(及其切片)复制到新列表:
L [1:4]将复制一个有限的部分,并且
L [:]将复制整个列表。
那种副本只有一个级别 - 副本的内容
将引用L的内容
字典有一个复制方法可以创建一个新词典。
同样这个副本只有一个级别。
复制模块提供了一种更通用的方法来复制对象。它b / b
提供了生成浅拷贝和深拷贝的能力。
一个小小的注释:在Python的十几年编程中,我用过
这些各种复制方法可能是十几次或更少。如果你发现
你经常复制结构,你可能没有有效地使用Python作为
。
Gary Herron
4月15日上午10:23,hall.j ... @ gmail.com写道:
作为Python的一个相对较新的人,我没有做过很多关于它的黑客攻击。我第一次尝试使用Python的古怪(至少是
我)倾向于通过引用而不是值来分配(我是
来自一个VBA世界,这就是我正在使用的术语。我感到惊讶的是,这两个案件表现得如此不同
test = [[1],[2]]
x = test [0]
x [0] = 5
test>> [[5],[2]]
x = 1
test
>> [[5],[2]]
x
> 1
现在我已经做了一点阅读,我想我明白了这个问题...
我的问题是,什么将某物的价值分配给新名称的最佳实践方式是什么?
ie
test = [[1,2],[3,4]]
我需要使用上面
列表中的第一个列表进行一些数据操作而不需要更改< test>
显然x = test [0]将无效,因为我所做的任何更改都会改变
或iginal ...
我发现我可以这样做:
x = [] +测试[0]
获得我是一个纯粹的 (即没有连接到测试[0])列表,但是
让我感到有点笨拙
感谢您的时间和帮助。
我认为你理解这个概念,基本上你想复制一份。
以太这些是可以接受的:
x = test [0] [:]
或
x = list(test [0])
这也有效:
导入复制
x =复制(测试[0])
马特
4月15日,6:23 * pm,hall.j ... @ gmail.com写道:
作为Python的一个相对较新的人,我没有做过很多关于它的黑客攻击。我第一次尝试使用Python的古怪(至少是
我)倾向于通过引用而不是值来分配(我是
来自一个VBA世界,这就是我正在使用的术语。我感到惊讶的是,这两种情况表现得如此不同
或许最好认为你绑定了名字''x''当你写''x = 42''时,对象
''42''。
test = [[1], [2]]
x = test [0]
x [0] = 5
test>> [[5],[2] ]
x = 1
test
> > [[5],[2]]
x
> 1
现在我已经做了一点阅读,我想我明白了这个问题...
我的问题是,将
值的某个值分配给新名称的最佳做法是什么?"
ie
test = [[1,2],[3,4]]
我需要使用上面的
列表中的第一个列表进行一些数据操作而不更改< test>
显然x = test [0]将无效,因为我所做的任何更改都将改变
原版...
我发现我可以这样做:
x = [] + test [0]
让我变得纯粹 (即没有连接到测试[0])列表,但是
让我感到有点笨拙
感谢您的时间和帮助。
要创建一个与序列seq具有相同元素的新列表,您可以使用list(seq)
。 ''list''是列表的类型,它也是列表对象的''构造函数'
(对于其他常见的buit-in类型也是如此,例如
为''int'',''float'',''str'',''tuple'',''dict'')。
例如>
>> foo = [1,2,3]
bar = list( foo)
foo [0] = 4
foo
[4,2,3]
>> foo = [1,2,3]
bar = list (foo)
bar [0] = 4
bar
[4,2,3]
>> foo
[1,2,3]
>>>
HTH
-
Arnaud
As a relative new comer to Python, I haven''t done a heck of a lot of
hacking around with it. I had my first run in with Python''s quirky (to
me at least) tendency to assign by reference rather than by value (I''m
coming from a VBA world so that''s the terminology I''m using). I was
surprised that these two cases behave so differently
test = [[1],[2]]
x = test[0]
x[0] = 5
test
>>[[5],[2]]
x = 1
test
>>>[[5],[2]]
x
>>1
Now I''ve done a little reading and I think I understand the problem...
My issue is, "What''s the ''best practise'' way of assigning just the
value of something to a new name?"
i.e.
test = [[1,2],[3,4]]
I need to do some data manipulation with the first list in the above
list without changing <test>
obviously x = test[0] will not work as any changes i make will alter
the original...
I found that I could do this:
x = [] + test[0]
that gets me a "pure" (i.e. unconnected to test[0] ) list but that
concerned me as a bit kludgy
Thanks for you time and help.
ha*******@gmail.com wrote:As a relative new comer to Python, I haven''t done a heck of a lot of
hacking around with it. I had my first run in with Python''s quirky (to
me at least) tendency to assign by reference rather than by value (I''m
coming from a VBA world so that''s the terminology I''m using). I was
surprised that these two cases behave so differently
test = [[1],[2]]
x = test[0]
x[0] = 5
test
>>>[[5],[2]]
x = 1
test
>>>[[5],[2]]
x
>>>1
Now I''ve done a little reading and I think I understand the problem...
My issue is, "What''s the ''best practise'' way of assigning just the
value of something to a new name?"
i.e.
test = [[1,2],[3,4]]
I need to do some data manipulation with the first list in the above
list without changing <test>
obviously x = test[0] will not work as any changes i make will alter
the original...
I found that I could do this:
x = [] + test[0]
that gets me a "pure" (i.e. unconnected to test[0] ) list but that
concerned me as a bit kludgy
Thanks for you time and help.
If you want a *copy* of an object (or portion thereof), you must
explicitly *specify* a copy. THere are different ways of doing that
depending on the object in question.
Lists (and slices thereof) can be copied to a new list withthe slice syntax:
L[1:4] will copy a limited portion, and
L[:] will copy the whole list.
The kind of copy is only one level deep -- the contents of the copy
will be references to the contents of L
Dictionaries have a copy method that creates a new dictionary.
Again this copy is only one level deep.
The copy modules provides a more general way to copy objects. It
provides the ability to produce both shallow and deep copies.
A small note: In a dozen years of programming in Python, I''ve used
these various copy methods perhaps a dozen times or less. If you find
you are copying structures often, you are probably not using Python as
effectively as you could.
Gary Herron
On Apr 15, 10:23 am, hall.j...@gmail.com wrote:As a relative new comer to Python, I haven''t done a heck of a lot of
hacking around with it. I had my first run in with Python''s quirky (to
me at least) tendency to assign by reference rather than by value (I''m
coming from a VBA world so that''s the terminology I''m using). I was
surprised that these two cases behave so differently
test = [[1],[2]]
x = test[0]
x[0] = 5
test>>[[5],[2]]
x = 1
test
>>[[5],[2]]
x
>1
Now I''ve done a little reading and I think I understand the problem...
My issue is, "What''s the ''best practise'' way of assigning just the
value of something to a new name?"
i.e.
test = [[1,2],[3,4]]
I need to do some data manipulation with the first list in the above
list without changing <test>
obviously x = test[0] will not work as any changes i make will alter
the original...
I found that I could do this:
x = [] + test[0]
that gets me a "pure" (i.e. unconnected to test[0] ) list but that
concerned me as a bit kludgy
Thanks for you time and help.
I think you understand the concept, basically you want to make a copy.
Ether of these are acceptable:
x = test[0][:]
OR
x = list(test[0])
this will also work:
import copy
x = copy(test[0])
Matt
On Apr 15, 6:23*pm, hall.j...@gmail.com wrote:As a relative new comer to Python, I haven''t done a heck of a lot of
hacking around with it. I had my first run in with Python''s quirky (to
me at least) tendency to assign by reference rather than by value (I''m
coming from a VBA world so that''s the terminology I''m using). I was
surprised that these two cases behave so differentlyPerhaps it is better to think that you bind the name ''x'' to the object
''42'' when you write ''x=42''.
test = [[1],[2]]
x = test[0]
x[0] = 5
test>>[[5],[2]]
x = 1
test
>>[[5],[2]]
x
>1
Now I''ve done a little reading and I think I understand the problem...
My issue is, "What''s the ''best practise'' way of assigning just the
value of something to a new name?"
i.e.
test = [[1,2],[3,4]]
I need to do some data manipulation with the first list in the above
list without changing <test>
obviously x = test[0] will not work as any changes i make will alter
the original...
I found that I could do this:
x = [] + test[0]
that gets me a "pure" (i.e. unconnected to test[0] ) list but that
concerned me as a bit kludgy
Thanks for you time and help.To create a new list with the same elements as a sequence seq, you can
use list(seq). ''list'' is the type of lists, it is also a ''constructor''
for list objects (the same goes for other common buit-in types, such
as ''int'', ''float'', ''str'', ''tuple'', ''dict'').
E.g.
>>foo = [1, 2, 3]
bar = list(foo)
foo[0] = 4
foo
[4, 2, 3]
>>foo = [1, 2, 3]
bar = list(foo)
bar[0] = 4
bar
[4, 2, 3]
>>foo
[1, 2, 3]
>>>
HTH
--
Arnaud
这篇关于用于“按值分配”的优选方法。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!