[Zope] Python Q: dynamic dictionary of dictionaries..

marc lindahl marc@bowery.com
Mon, 05 Nov 2001 14:23:22 -0500


This could be a 'deep copy' issue - check the python.org website.

Other thing you could try is:

hash_one = {}
for i in my_list:
    hash_one[i] = test

The way you had it, you're chucking the old hash_one every iteration, and
re-assigning it to a fresh dictionary {i:""}

> From: <zope@virtosi.com>
> Date: Mon, 5 Nov 2001 14:21:14 -0500 (EST)
> To: <zope@zope.org>
> Subject: [Zope] Python Q: dynamic dictionary of dictionaries..
> 
> this is more purely a python question as i am using external methods for
> most of my processing.... SOS,,, perl guy getting squeezed by the snake.
> 
> so this seems like it should be trivial but as a perl hack just learing,
> i'm not sure of the syntax, or if i am missing something basic about the
> dictionary type.
> 
> so, i have a list of strings that will be used as keys, say:
>>>> my_list['K1','K2','K3']
> 
> for each of those keys, i would like to create a dictionary that has as
> its value a dictionary that like  the following:
> 
>>>> test = {'us_lead': 0,
>>>> 'foreign_lead': 0,
>>>> 'us_gov_lead': 0,
>>>> 'foreign_lead': 0,
>>>> 'us_other': 0,
>>>> 'foreign_other': 0 }
> 
>>>> for i in my_list:
>>>> hash_one = {i: ""}
>>>> hash_one[i] = test
> 
> this seems to "reset" the value of has_one with each iteration instead of
> updating it?? the key should be different, why is this??
> 
> 
> 
> -- thanks for all the love,
> james jackson (aka, jcj@slack.net)
> 
> 
> 
> 
> _______________________________________________
> Zope maillist  -  Zope@zope.org
> http://lists.zope.org/mailman/listinfo/zope
> **   No cross posts or HTML encoding!  **
> (Related lists - 
> http://lists.zope.org/mailman/listinfo/zope-announce
> http://lists.zope.org/mailman/listinfo/zope-dev )