[Tutor] accessing items in nested structure
kevin parks
kp8 at mac.com
Mon Mar 22 05:13:10 EST 2004
def datas(x):
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] },
# { (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] }]
return pg01[x]
#
def pctrans(seq, transposition):
'''pctrans -- transposes pc sequence'''
output = []
for item in seq:
output.append((item + transposition) % 12)
return output
#
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
want to be able to access each key and its list, that its i want to be
able to iterate through this pg01 list and do something to each key and
value(list) in the list.
Like for example print the key tuple and print the list followed by the
list again but sorted, then list again modified by function call to the
pctrans() func above (just to pick something at random for now) or sort
or something. SO that i might get on output:
1,0 [8, 3, 5, 4] [3, 4, 5, 8 ] [4, 5, 6, 9]
etc....
I can iterate over the list and print each item, like so in the
interpreter:
>>> import data
>>> x = data.datas(1)
>>> x
{(7, 0): [4, 3, 2, 2]}
but i am not sure how to get inside the dictionaries inside the pg01
and iterate over those and the nested list values.
Also since my dictionary keys are not consecutive numbers how can i be
sure to hit them all?
cheers,
-kp--
More information about the Tutor
mailing list