dictionary help

MRAB python at mrabarnett.plus.com
Tue Aug 11 23:11:57 CEST 2009


Krishna Pacifici wrote:
> Wow, thanks MRAB and Simon, you guys are good.
> 
> I guess I will go ahead and ask the next question that has also stumped 
> me for awhile now.
> 
> So basically I need to loop through the values in the new dictionary and 
> append attributes of a class object.  Each of the values (and keys) 
> represent a block in a grid with a specific location (e.g. 35 is the 
> block in row 3 col 5) and each block is an object with several 
> attributes associated with it.  I want to call that block and append two 
> separate attributes to the dictionary within that same key.
> 
> So again it would look something like this:
> 
> block 35 has 2 attributes, say a and b, a=2, b=5
> block 37 has a=1, b=3
> block 46 has a=3, b=8
> 
> the two attributes come from two different definitions within the class 
> statement,
> def detections
> ...
> return a
> 
> def abundance
> ...
> return b
> 
> so I would want to append to key 36 those two attributes for each block 
> so that the resulting dictionary item would look like this:
>                          a           b  
> {36:[35,37,46], [2,1,3], [5,3,8] ...}
> 
That doesn't look like a dictionary. Perhaps want you want is for the
value to be a list of lists:

     {36: [[35,37,46], [2,1,3], [5,3,8]] ...}

although you'd have parallel lists, ie a list of blocks, a list of 'a',
and a list of 'b'. A better format might be to keep a block's attributes
with the block itself.

     {36: [[35,2,5], [37,1,3], [46,3,8]] ...}

> Any help with this would be greatly appreciated.  And thank you so much 
> for all of your help thus far, I'm still pretty new to python and am 
> enjoying all of the flexibility associated with a scripting and 
> programming language.
> 
[snip]




More information about the Python-list mailing list