Re: Deeper copy than deepcopy

2009-10-28 Thread Scott Pakin
Thanks everyone! The list-comprehension approach should work in my case as I know a priori that my data structure is finite. Still, it'd be awfully convenient for Python's copy module to offer a copy.deepercopy function -- even if it comes with the caveat that it will explode if given a data

Re: Deeper copy than deepcopy

2009-10-27 Thread Steven D'Aprano
On Tue, 27 Oct 2009 15:16:15 -0700, Gary Herron wrote: Try this: d = [1,2,3] r = [[x for x in d] for i in range(3)] r[1][2] = 999 r [[1, 2, 3], [1, 2, 999], [1, 2, 3]] [x for x in d] is better written as d[:] -- Steven -- http://mail.python.org/mailman/listinfo/python-list

Deeper copy than deepcopy

2009-10-27 Thread Scott Pakin
copy.deepcopy apparently preserves multiple references to the same object: $ python Python 2.5.2 (r252:60911, Jan 4 2009, 17:40:26) [GCC 4.3.2] on linux2 Type help, copyright, credits or license for more information. import copy d = [1,2,3] r = copy.deepcopy([d]*3)

Re: Deeper copy than deepcopy

2009-10-27 Thread Cameron Simpson
On 27Oct2009 14:01, Scott Pakin scott+...@pakin.org wrote: | copy.deepcopy apparently preserves multiple references to the same object: | | $ python | Python 2.5.2 (r252:60911, Jan 4 2009, 17:40:26) | [GCC 4.3.2] on linux2 | Type help, copyright, credits or license for more

Re: Deeper copy than deepcopy

2009-10-27 Thread Cameron Simpson
On 28Oct2009 08:23, I wrote: | | I wanted to wind up with r being [[1, 2, 3], [1, 2, 999], [1, 2, 3]]. | | What's the right way to construct r as a list of *independent* d lists? | | Well, you would need to write your own. But consider this: | x = [1, 2] | x.append(x) | Your deepercopy()

Re: Deeper copy than deepcopy

2009-10-27 Thread Gary Herron
Scott Pakin wrote: copy.deepcopy apparently preserves multiple references to the same object: $ python Python 2.5.2 (r252:60911, Jan 4 2009, 17:40:26) [GCC 4.3.2] on linux2 Type help, copyright, credits or license for more information. import copy d = [1,2,3] r =