Em Quinta 20 Julho 2006 04:51, linda.s escreveu: > But in the following example, a change in a spread to both b and c: > >>> a=[[1,2,3], [4,5,6]] > >>> b=a > >>> c=copy.copy(a) > >>> a[0][0]='a' > >>> a > > [['a', 2, 3], [4, 5, 6]] > > >>> b > > [['a', 2, 3], [4, 5, 6]] > > >>> c > > [['a', 2, 3], [4, 5, 6]]
Linda, re-read Danny's mail, it's all there! But I'll try again. Your first line, >>> a = [ [1,2,3], [4,5,6] ] could as well be written as: >>> a0 = [1,2,3] >>> a1 = [4,5,6] >>> a = [a0, a1] so: >>> b=a >>> import copy And now what is a[0][0]? It's obviously a0[0]. And even if c is a different object, c[0][0] is also a0[0]. If you say now that c[0] is no more a0, but any other thing, a will not be changed. But if you change a0, directly or through the c or a lists, c *and* a first item will be changed. See it in action: In [1]: a0 = [1,2,3] In [2]: a1 = [4,5,6] In [3]: import copy In [4]: a = [a0,a1] In [5]: b=a In [6]: c = copy.copy(a) In [7]: a0[0] = 25 In [8]: a Out[8]: [[25, 2, 3], [4, 5, 6]] In [9]: b Out[9]: [[25, 2, 3], [4, 5, 6]] In [10]: c Out[10]: [[25, 2, 3], [4, 5, 6]] In [11]: c[0] = [7,8,9] In [12]: a Out[12]: [[25, 2, 3], [4, 5, 6]] In [13]: b Out[13]: [[25, 2, 3], [4, 5, 6]] In [14]: c Out[14]: [[7, 8, 9], [4, 5, 6]] In [15]: a[0][0] = 92 In [16]: a Out[16]: [[92, 2, 3], [4, 5, 6]] In [17]: b Out[17]: [[92, 2, 3], [4, 5, 6]] In [18]: c Out[18]: [[7, 8, 9], [4, 5, 6]] Tiago. _______________________________________________ Tutor maillist - [email protected] http://mail.python.org/mailman/listinfo/tutor
