[Tutor] accessing items in nested structure

Crabtree, Chad Chad.Crabtree at nationalcity.com
Tue Mar 23 10:47:18 EST 2004



-----Original Message-----
From: kevin parks [mailto:kp8 at mac.com]
Sent: Monday, March 22, 2004 5:13 PM
To: Crabtree, Chad
Subject: Re: [Tutor] accessing items in nested structure


Hi Chad,

Thanks for this. I really appreciate your taking the time to answer.  
However i get the impression that there is something here i don't get.  
I think when you say dict.items(): that is kind of an abstraction  
right? Cause if i type that actually thing in the interpreter i get:

 >>> data.datas()
Traceback (most recent call last):
   File "<stdin>", line 1, in ?
   File "/Users/kevin/Desktop/data.py", line 27, in datas
     for key,value in dict.items():
TypeError: descriptor 'items' of 'dict' object needs an argument


clearly i am supposed to plug something in someplace right?

This doesn't seem to work either:

def datas():
     page02 = [    { (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] },
             # { (25,0) : [5] },
             # { (27,0) : [0] },
             { (28,0) : [10, 0, 5, 7] },
             { (29,0) : [10, 11] },
             { (30,0) : [8, 3, 5, 4] },
             { (32,0) : [5, 0, 10, 7] },
             { (34,0) : [8, 3, 7, 9] },
             { (36,0) : [5, 4, 3, 1] },
             { (36,1) : [5, 4, 3, 1, 7] },
             { (37,0) : [0, 8, 2, 4, 9, 10, 1] },
             { (37,1) : [0, 8, 2, 4, 9, 10, 1, 6] },
             { (39,0) : [8, 10, 1, 9] },
             { (39,1) : [8, 10, 1, 9, 7] },
             { (41,0) : [2, 0, 3, 1] },
             { (41,1) : [2, 0, 3, 1, 6] }]
     for x in page02:
         for key,value in dict.items():
             print "Key=%s" %key
             print "Value=%s" %str(value)


Okay I'm sory.  'Dict' is the name of the viarble dictionary.

So it should by <anydictionaryname>.items()

I'll try again.  I failed to paste all the pertinent iformation.

>>> dict={'h':[1,1,1,1],'e':[2,2,2,2],'l':[3,3,3,3],'o':[4,4,4,4]}
>>> for key,value in dict.items():
	print "Key=%s" %key,
	print "\tValue=%s" %str(value)

	
Key=h 	Value=[1, 1, 1, 1]
Key=e 	Value=[2, 2, 2, 2]
Key=l 	Value=[3, 3, 3, 3]
Key=o 	Value=[4, 4, 4, 4]
>>> 

I'v made some changes to you dictionary
>>> page02 = {
	     (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] ,
             (28,0) : [10, 0, 5, 7] ,
             (29,0) : [10, 11] ,
             (30,0) : [8, 3, 5, 4] ,
             (32,0) : [5, 0, 10, 7] ,
             (34,0) : [8, 3, 7, 9] ,
             (36,0) : [5, 4, 3, 1] ,
             (36,1) : [5, 4, 3, 1, 7] ,
             (37,0) : [0, 8, 2, 4, 9, 10, 1] ,
             (37,1) : [0, 8, 2, 4, 9, 10, 1, 6] ,
             (39,0) : [8, 10, 1, 9] ,
             (39,1) : [8, 10, 1, 9, 7] ,
             (41,0) : [2, 0, 3, 1] ,
             (41,1) : [2, 0, 3, 1, 6] }
>>> for key, value in page02.items():
	print "Key=%s" %str(key),
	print "\tValue=%s" %str(value)

	
Key=(30, 0) 	Value=[8, 3, 5, 4]
Key=(41, 0) 	Value=[2, 0, 3, 1]
Key=(37, 1) 	Value=[0, 8, 2, 4, 9, 10, 1, 6]
Key=(37, 0) 	Value=[0, 8, 2, 4, 9, 10, 1]
Key=(36, 0) 	Value=[5, 4, 3, 1]
Key=(36, 1) 	Value=[5, 4, 3, 1, 7]
Key=(18, 0) 	Value=[10, 2, 8, 7]
Key=(39, 1) 	Value=[8, 10, 1, 9, 7]
Key=(7, 0) 	Value=[4, 3, 2, 2]
Key=(14, 0) 	Value=[8, 3, 5, 4]
Key=(24, 0) 	Value=[7, 9, 3, 8]
Key=(20, 0) 	Value=[10, 0, 5, 7]
Key=(22, 0) 	Value=[10, 2, 8, 7]
Key=(1, 0) 	Value=[8, 3, 5, 4]
Key=(32, 0) 	Value=[5, 0, 10, 7]
Key=(39, 0) 	Value=[8, 10, 1, 9]
Key=(34, 0) 	Value=[8, 3, 7, 9]
Key=(29, 0) 	Value=[10, 11]
Key=(28, 0) 	Value=[10, 0, 5, 7]
Key=(41, 1) 	Value=[2, 0, 3, 1, 6]

what you had was a list of single key dictionaries.  Which was obfuscating
the data abstraction.
What I chaged it to was a dictionary with each key pointing to a List. This
way you will not have to first
(as you had it) Iterate of a list, each item being a single item dictionary.
Then find the key with some function then iterate of the list

example

def runOverit(aList):
	for x in aList:
		for k in x.keys():
			for z in k:
				print z # or do something with this value.

I believe you where improperly nesting the container types for the job you
are trying to do.




-------------------------------------------------------------------------------------------
***National City made the following annotations
-------------------------------------------------------------------------------------------

This communication is a confidential and proprietary business communication.
It is intended solely for the use of the designated recipient(s).  If this
communication is received in error, please contact the sender and delete this
communication.
===========================================================================================



More information about the Tutor mailing list