dictionary - Building up a list of dictionaries from a few lists in Python -


i have few lists this:

pargs = [args.pee, args.pem,...,args.pet] # 9 elements of boolean type indices = [(0,0),(0,1),...,(2,2)] # 9 possible combinations (i,j), = 0,1,2; j = 0,1,2 d = [{},{},...,{}] # 9 dictionaries, desired result 

the result want see should this:

d = [{event:args.pee,i:0,j:0},{event:args.pem,i:0,j:1},...{event: args.pet,i:2,j:2}] 

the dictionaries must ordered shown above.

i tried

for d in d:     in range(3):         j in range(3):             d['i'],d['j'] = i,j 

but not trick. i've tried numerous algorithms zip(),product(),dict(), no avail...

with comprehension , ordereddict, demo:

from collections import ordereddict pargs = ['arg1', 'arg2', 'arg3', 'arg4', 'arg5', 'arg6', 'arg7', 'arg8', 'arg9'] indices = ((x,y) x in range(3) y in range(3)) result = [ordereddict([('event',p), ('i',i), ('j',j)]) p,(i,j) in zip(pargs, indices)] print(result) # [ordereddict([('event', 'arg1'), ('i', 0), ('j', 0)]), ordereddict([('event', 'arg2'), ('i', 0), ('j', 1)]), ordereddict([('event', 'arg3'), ('i', 0), ('j', 2)]), ordereddict([('event', 'arg4'), ('i', 1), ('j', 0)]), ordereddict([('event', 'arg5'), ('i', 1), ('j', 1)]), ordereddict([('event', 'arg6'), ('i', 1), ('j', 2)]), ordereddict([('event', 'arg7'), ('i', 2), ('j', 0)]), ordereddict([('event', 'arg8'), ('i', 2), ('j', 1)]), ordereddict([('event', 'arg9'), ('i', 2), ('j', 2)])] 

edit

if misunderstood requirements , order within dictionaries not matter, can without ordereddict this:

result = [{'event':p, 'i':i, 'j':j} p,(i,j) in zip(pargs, indices)] 

Comments

Popular posts from this blog

sublimetext3 - what keyboard shortcut is to comment/uncomment for this script tag in sublime -

java - No use of nillable="0" in SOAP Webservice -

ubuntu - Laravel 5.2 quickstart guide gives Not Found Error -