Copmlex structures....

Chris Liechti cliechti at gmx.net
Fri Mar 22 17:13:33 EST 2002


"Dmitry S. Makovey" <dmitry at athabascau.ca> wrote in
news:iUMm8.141$%52.3298 at jekyl.ab.tac.net: 
> Hi. The problem itself is - I want to use complex structures like
> described below, but i want it another way....
> 
> 01. >>> f={}
> 02. >>> f['name']='dimon'
> 03. >>> f['age']=20
> 04. >>> a=[]
> 05. >>> a.append(f)
> 06. >>> print a
> 07. [{'age': 20, 'name': 'dimon'}]
> 08. >>> f['age']=22
> 09. >>> print a
> 10. [{'age': 22, 'name': 'dimon'}]
> 
> I don't like result in the last like I've expected to have [{'age': 
> 20, 'name': 'dimon'}], how could i do this?

it's what i would expect :-)
the list stores a reference to the one and only dictionary object and 
you can get there through the list or through the old name.

> I know that it must be as simple as using something like b[:], but
> what?

that would only give you a copy of the list with copies of all 
references (flat-copy). what you need is a deep-copy that dives and 
copies recursively. look at the "copy" module for such functionality 
and/or serach groups.google.com for "deep copy"

chris


-- 
Chris <cliechti at gmx.net>




More information about the Python-list mailing list