complex data structure - insert value

spar spariam at hotmail.com
Sun Oct 10 19:08:21 CEST 2004


Nick Craig-Wood <nick at craig-wood.com> wrote in message news:<slrncmhu61.aqa.nick at irishsea.home.craig-wood.com>...
 
> def somefunc(a,b): return 1
> bars=[1, 2, 3, 4, 5]
> els=[7, 8]
> 
> data = []
> for bar_count in bars:
>   for el_count in els:
>     var = somefunc(bar_count, el_count)
>     while bar_count >= len(data):
>       data.append({})
>     l = data[bar_count].setdefault('CC', [])
>     while el_count >= len(l):
>       l.append(None)
>     l[el_count] = var
> 
> 
> >>> pprint(data)
> [{},
>  {'CC': [None, None, None, None, None, None, None, 1, 1]},
>  {'CC': [None, None, None, None, None, None, None, 1, 1]},
>  {'CC': [None, None, None, None, None, None, None, 1, 1]},
>  {'CC': [None, None, None, None, None, None, None, 1, 1]},
>  {'CC': [None, None, None, None, None, None, None, 1, 1]}]


Thanks, this is close...I'll have to play with it a bit. I've done
Perl for quite sometime, but am new to Python.

My example code didn't exactly show what I'm trying to do. The data
structure should actually look something like this (think of each
array element as a musical bar, each dict element ('HH', etc.) is an
instrument in that bar, and the list within the dict represent beats
for that instrument within the bar):

[{},
 {'CC': ['-', '-', '-', '-'], 'HH': ['x', 'x', 'x', 'x'], 'SD': ['-',
'-', 'o', '-']},
{'CC': ['x', '-', '-', '-'], 'HH': ['x', 'x', 'x', 'x'], 'SD': ['o',
'-', 'o', '-']},
{'CC': ['-', '-', '-', '-'], 'HH': ['x', 'x', 'x', 'x'], 'SD': ['-',
'-', 'o', '-']},
{'CC': ['x', '-', '-', '-'], 'HH': ['x', 'x', 'x', 'x'], 'SD': ['o',
'-', 'o', '-']}]
 
So this shows bars 1-4 (there is no bar 0, so it's empty), each bar
has three instruments ('CC', 'HH', and 'SD') and each instrument shows
what is to be played on beats (subdivisions) 1-4 (actually there will
be 16 subdivisions representing 16 16th notes).

I think I can take your code and mold it into what I need. Thanks...



More information about the Python-list mailing list