为什么更改1列表会影响另一个? [英] Why does changing 1 list affect the other?

查看:105
本文介绍了为什么更改1列表会影响另一个?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对于python来说,我是一个新手,当今天使用

列表时,我注意到一些非常奇怪的行为,任何建议

欢迎:


Python 2.2.3(#1,2003年11月6日,14:12:38)

[GCC 3.3.2 20031022(Gentoo Linux 3.3.2-r2) ,bootolice)] on linux2

输入help,copyright,credit等等。或许可证或更多信息。

firstlist = [''item1'',''item2'',''item2'']
secondlist = firstlist
print(firstlist,secondlist)
([''item1'',''item2'',''item2''],[''item1'','' item2'',''item2''])firstlist [0] =''strangeness''
print(firstlist,secondlist)
([''strangeness'',''item2'',' 'item2''],[''strangeness'',''item2'',''item2''])




为什么改变一个列表会影响另一个列表?它驱使我

疯了!

解决方案

oom< oo *@xxnospamxx.ps.gen .NZ>写道:

当谈到python时,我有点新手,今天使用
列表我注意到一些非常奇怪的行为,任何建议
欢迎:

Python 2.2.3(#1,2003年11月6日,14:12:38)
[GCC 3.3.2 20031022(Gentoo Linux 3.3.2-r2,propolice)] on linux2
输入help,copyright,credit等。或许可证或更多信息。

firstlist = [''item1'',''item2'',''item2'']
secondlist = firstlist
print(firstlist,secondlist)([''item1'',''item2'',''item2''],[''item1'',''item2'',''item2''] firstlist [0] =''陌生''
打印(firstlist,secondlist)([''strangeness'',''item2'',''item2''],[''strangeness'''''item2 '',''item2''])



为什么改变一个列表会影响另一个列表?这让我感到疯狂!




因为当你说secondlist = firstlist时,你没有复制
$ b $在列表中,您只是对现有列表另外引用

对象。如果你已经习惯了C / C ++,可以把它想象成传递一个指针




如果你真的想要制作一个新的列表,你应该看看副本

模块(特别是copy.deepcopy)。或者,有点简单,你可以

刚刚说了secondlist = list(firstlist),它创建了一个新的

(有点像复制构造函数可能在C ++中做的) 。


On Thu,2003年11月6日16:36:08 +1300,oom写道:

< blockquote class =post_quotes> firstlist = [''item1'',''item2'',''item2'']


创建一个包含三个字符串对象的列表对象,并将

名称''firstlist''绑定到列表对象。

secondlist = firstlist


绑定名称''第二列表''到同一个列表对象。

print(firstlist,secondlist)([''item1'',''item2'',''item2''],[''item1'' ,''item2'',''item2''])


输出相同的列表对象两次,因为它绑定到两个

''firstlist ''和''secondlist''。

firstlist [0] =''strangeness''


改变列表对象。

print(firstlist,secondlist)([' 'strangeness',''item2'',''item2''],[''strangeness'',''item2'',''item2''])




两次输出相同的列表对象,因为它绑定到
''firstlist''和''secondlist''。

为什么改变一个列表影响其他名单?它让我感到疯狂!




因为只有一个列表,有两个不同的名字。这是''secondlist = firstlist''的结果。


您可能希望操作系统获取列表对象的*副本*,以及绑定

''secondlist''到那个新对象。对于某些

类型(例如标量)而言,这会自动发生,但不会出现列表或dicts或其他结构化类型。

import copy
firstlist = [' 'item1'',''item2'',''item3'']
secondlist = copy.copy(firstlist)
print(firstlist,secondlist)
([''item1''' ,''item2'',''item3''],[''item1'',''item2'',''item3''])firstlist [0] =''no_strangeness''
print( firstlist,secondlist)
([''no_strangeness'',''item2'',''item3''],[''item1'',''item2'',''item3''])




-

\很难相信一个人在说实话的时候你了|

` \如果你在他的位置,你就会撒谎。 - Henry L. |

_o__)Mencken |

Ben Finney< http://bignose.squidly.org/>


2003年11月5日星期三23:05:30 -0500,Roy Smith< ro*@panix.com>写道:

oom< oo *@xxnospamxx.ps.gen.nz>写道:

当谈到python时,我有点新手,今天使用
列表我注意到一些非常奇怪的行为,任何建议
欢迎:

Python 2.2.3(#1,2003年11月6日,14:12:38)
[GCC 3.3.2 20031022(Gentoo Linux 3.3.2-r2,propolice)] on linux2
输入help,copyright,credit等。或许可证或有关更多信息。

>>> firstlist = [''item1'',''item2'',''item2'']
>>> secondlist = firstlist
>>> print(firstlist,secondlist)


([''item1'',''item2'',''item2''],[''item1'',''item2'','' item2''])

>>> firstlist [0] =''陌生''
>>> print(firstlist,secondlist)


([''strangeness'',''item2'',''item2''],[''strangeness'''''item2'''''' item2''])

>>>



为什么更改一个列表会影响另一个列表?它让我感到疯狂!



因为当你说secondlist = firstlist时,你没有复制
列表,你是只是另外引用现有的列表
对象。如果你已经习惯了C / C ++,可以把它想象成一个指针。

如果你真的想要制作一个新的列表,你应该看一下这个副本。 />模块(特别是copy.deepcopy)。或者,稍微简单一点,你可以刚刚说了secondlist = list(firstlist),它创建了一个新的
(有点像复制构造函数可能在C ++中做的那样)。




非常感谢Ben和Roy !!


我怀疑这样的事情正在发生,但是头脑是什么?
$ b $我抓了一个刮刮的会议!


这是一个非常活跃的NG ;-)


问题解决了


I am a bit of a newbie when it comes to python, when working with
lists today I noticed some very odd behaviour, any suggestions
welcome:

Python 2.2.3 (#1, Nov 6 2003, 14:12:38)
[GCC 3.3.2 20031022 (Gentoo Linux 3.3.2-r2, propolice)] on linux2
Type "help", "copyright", "credits" or "license" for more information.

firstlist=[''item1'',''item2'',''item2'']
secondlist=firstlist
print (firstlist,secondlist) ([''item1'', ''item2'', ''item2''], [''item1'', ''item2'', ''item2'']) firstlist[0]=''strangeness''
print (firstlist,secondlist) ([''strangeness'', ''item2'', ''item2''], [''strangeness'', ''item2'', ''item2''])



why does altering one list affect the other list ? it is driving me
insane!

解决方案

oom <oo*@xxnospamxx.ps.gen.nz> wrote:

I am a bit of a newbie when it comes to python, when working with
lists today I noticed some very odd behaviour, any suggestions
welcome:

Python 2.2.3 (#1, Nov 6 2003, 14:12:38)
[GCC 3.3.2 20031022 (Gentoo Linux 3.3.2-r2, propolice)] on linux2
Type "help", "copyright", "credits" or "license" for more information.

firstlist=[''item1'',''item2'',''item2'']
secondlist=firstlist
print (firstlist,secondlist) ([''item1'', ''item2'', ''item2''], [''item1'', ''item2'', ''item2'']) firstlist[0]=''strangeness''
print (firstlist,secondlist) ([''strangeness'', ''item2'', ''item2''], [''strangeness'', ''item2'', ''item2''])



why does altering one list affect the other list ? it is driving me
insane!



Because when you say secondlist = firstlist, you''re not making a copy of
the list, you''re just making another reference to the existing list
object. If you''re used to C/C++, think of it as passing a pointer
around.

If you really wanted to make a new list, you should look at the copy
module (specifically copy.deepcopy). Or, somewhat simplier, you could
have just said secondlist = list (firstlist), which creates a new one
(kind of like a copy constructor might do in C++).


On Thu, 06 Nov 2003 16:36:08 +1300, oom wrote:

firstlist=[''item1'',''item2'',''item2'']
Creates a list object, containing three string objects, and binds the
name ''firstlist'' to the list object.
secondlist=firstlist
Binds the name ''secondlist'' to the same list object.
print (firstlist,secondlist) ([''item1'', ''item2'', ''item2''], [''item1'', ''item2'', ''item2''])
Outputs the same list object twice, since it is bound to both
''firstlist'' and ''secondlist''.

firstlist[0]=''strangeness''
Alters the list object.
print (firstlist,secondlist) ([''strangeness'', ''item2'', ''item2''], [''strangeness'', ''item2'', ''item2''])



Outputs the same list object twice, since it is bound to both
''firstlist'' and ''secondlist''.
why does altering one list affect the other list ? it is driving me
insane!



Because there''s only one list, with two different names. This is a
result of ''secondlist = firstlist''.

What you probably want os to take a *copy* of the list object, and bind
''secondlist'' to that new object. This occurs automatically for some
types (e.g. scalars) but not lists or dicts or other structured types.

import copy
firstlist = [ ''item1'', ''item2'', ''item3'' ]
secondlist = copy.copy( firstlist )
print( firstlist, secondlist ) ([''item1'', ''item2'', ''item3''], [''item1'', ''item2'', ''item3'']) firstlist[0] = ''no_strangeness''
print( firstlist, secondlist ) ([''no_strangeness'', ''item2'', ''item3''], [''item1'', ''item2'', ''item3''])



--
\ "It is hard to believe that a man is telling the truth when you |
`\ know that you would lie if you were in his place." -- Henry L. |
_o__) Mencken |
Ben Finney <http://bignose.squidly.org/>


On Wed, 05 Nov 2003 23:05:30 -0500, Roy Smith <ro*@panix.com> wrote:

oom <oo*@xxnospamxx.ps.gen.nz> wrote:

I am a bit of a newbie when it comes to python, when working with
lists today I noticed some very odd behaviour, any suggestions
welcome:

Python 2.2.3 (#1, Nov 6 2003, 14:12:38)
[GCC 3.3.2 20031022 (Gentoo Linux 3.3.2-r2, propolice)] on linux2
Type "help", "copyright", "credits" or "license" for more information.

>>> firstlist=[''item1'',''item2'',''item2'']
>>> secondlist=firstlist
>>> print (firstlist,secondlist)


([''item1'', ''item2'', ''item2''], [''item1'', ''item2'', ''item2''])

>>> firstlist[0]=''strangeness''
>>> print (firstlist,secondlist)


([''strangeness'', ''item2'', ''item2''], [''strangeness'', ''item2'', ''item2''])

>>>



why does altering one list affect the other list ? it is driving me
insane!



Because when you say secondlist = firstlist, you''re not making a copy of
the list, you''re just making another reference to the existing list
object. If you''re used to C/C++, think of it as passing a pointer
around.

If you really wanted to make a new list, you should look at the copy
module (specifically copy.deepcopy). Or, somewhat simplier, you could
have just said secondlist = list (firstlist), which creates a new one
(kind of like a copy constructor might do in C++).



Well thanks to both Ben and Roy!!

I suspected something like this was going on, but what a head
scratching session I had!

This is a very active NG ;-)

problem solved


这篇关于为什么更改1列表会影响另一个?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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