[PIPython] copy() di dizionari

Carlo C8E Miron carlo.miron
Gio 28 Apr 2005 19:29:17 CEST


Il 28/04/05, M.Buso<mbposta a postino.it> ha scritto:
> a={ 
>     "a":1, 
>     "b":2, 
>     "c": { # dizionario interno 
>             "a1":10, 
>             "b1":20, 
>                         } 
>         } 
>   
> b=a.copy() 
>  
> a["c"]["a1"]="99"
>  
> print b["c"]["a1"]
> 99  
>   
> Come si puo' evitare questo? 

vedi >>> help("copy")
>>> import copy
>>> b = copy.deepcopy(a)

> come mai copy() non funziona con i dizionari interni? 

>>> help(dict.copy)
Help on method_descriptor:
copy(...)
    D.copy() -> a shallow copy of D

> Qualcuno puo' darmi qualche spiegazione. 

il metodo effettua una copia "superficiale", piu' veloce e quindi
migliore come default. per gli altri casi c'e' il modulo copy.

> Grazie 

HTH,
(c)

-- 
Disclaimer:
If I receive a message from you, you are agreeing that:
1. I am by definition, "the intended recipient".
2. All information in the email is mine to do with as I see fit and
 make such financial profit, political mileage, or good joke as it
 lends itself to. In particular, I may quote it on USENET or the WWW.
3. I may take the contents as representing the views of your company.
4. This overrides any disclaimer or statement of confidentiality that
 may be included on your message.


More information about the Python mailing list