[Tutor] accessing items in nested structure

kevin parks kp8 at mac.com
Mon Mar 22 17:00:13 EST 2004


On Monday, March 22, 2004, at 11:13  AM, tutor-request at python.org wrote:
>
>
> On 22 Mar 2004, kevin parks <- kp8 at mac.com wrote:
>
>> def datas(x):
>>      pg01 = [    { (1,0) : [8, 3, 5, 4] },
>>              { (7,0) : [4, 3, 2, 2] },
> [...]
>>              { (41,1) : [2, 0, 3, 1, 6] }]
>>      return pg01[x]
>> #
>
> [...]
>> Hi all. My data structure is getting a little complex with a list of
>> dictionaries (which are made up of a tuple key and list value). Now i
>
> Before we go into detail.  Are you sure you need such a complex data
> structure?  Why don't you just take one dictionary and put the 
> key/value
> pairs in it?  At the moment each dictionary has only one key.  That's a
> huge overhead.
>

Oh.... Uhmm, now that i think about it, there really isn't a good 
reason except that there are
several pages (here is just a portion of page 01) and i just broke it 
up that way. You are right
though it probably could be simpler.

> If that is cleared it will be easier to answer your question how to
> iterate over the structure.
>
> Why do you write a function to store the structure?

just because it is huge. It is about 15 pages of data and this is only 
a small snippit
of one list of one page to give an example. When the data gets like 
this i sometimes like to
just put it in its own file and import it so that i can just see what 
my code is without all that data
cluttering my screen.


originally i was doing something like this:

     m1 = [8, 3, 5, 4]
     m7 = [4, 3, 2, 2]
     m14 = [8, 3, 5, 4]
     m18 = [10, 2, 8, 7]
     m20 = [10, 0, 5, 7]
     m22 = [10, 2, 8, 7]
     m24 = [7, 9, 3, 8]

     page01 = [m1, m7, m14, m18, m20, m22, m24]

(followed by a bunch of code that munched on those lists)

But then i realized that i wanted to use that variable name too since 
it is meaningful so i changed that to:

     pg01 = [    { (1,0) : [8, 3, 5, 4] },
             { (7,0) : [4, 3, 2, 2] },
             { (14,0) : [8, 3, 5, 4] },
             { (18,0) : [10, 2, 8, 7] },
             { (20,0) : [10, 0, 5, 7] },
             { (22,0) : [10, 2, 8, 7] },
             { (24,0) : [7, 9, 3, 8] }]

So what i have here is page number (corresponds to another document 
being analyzed)
a line number (1, 7, 14, .....) and an item number (1,0), (7,0), (14,0) 
.... in this case all 0, but in some cases
there are several items like (82,0), (82,1), (82,3) for :

     (82,0) = [7,2,6,8]
     (82,1) = [10,9,0,11]
     (82,2) = [10,9,0,11,4]

-kevin--




More information about the Tutor mailing list