creating and appending to a dictionary of a list of lists
pyscottishguy at hotmail.com
pyscottishguy at hotmail.com
Tue Aug 14 22:30:44 EDT 2007
Hey,
I started with this:
factByClass = {}
def update(key, x0, x1, x2, x3):
x = factByClass.setdefault(key, [ [], [], [], [] ])
x[0].append(x0)
x[1].append(x1)
x[2].append(x2)
x[3].append(x3)
update('one', 1, 2, 3, 4)
update('one', 5, 6, 7, 8)
update('two', 9, 10, 11, 12)
print factByClass
{'two': [[9], [10], [11], [12]], 'one': [[1, 5], [2, 6], [3, 7], [4,
8]]}
I then 'upgraded' to this:
def update(key, *args):
x = factByClass.setdefault(key, [[], [], [], [] ])
for i, v in enumerate(args):
x[i].append(v)
Is there a better way?
Cheers!
More information about the Python-list
mailing list