python - list with xyz values to dictionary -
i have list:
list = [ '1', '1,'1', '-1','1','-1' ]
and need convert dictionary of dictionaries. first 3 values in x y z , second set 3 values x y z. result should be:
d = { 0:{x:1,y:1,z:1}, 1:{x:-1,y:1,z:-1}}
my attempt:
mylist=[1,1,1,-1,1,-1] count = 1 keycount = 0 l = {'x':' ','y':' ', 'z':' '} t = {} 1 in mylist: if count == 1: l['x'] = 1 print l if count == 2: l['y'] = 1 print l if count == 3: l['z'] = 1 print l count = 0 t[keycount] = l l = {} keycount += 1 count = count + 1 print t
but in result switches of keys of dictionary? have better solution?
a bit complicated one:
l = [ '1', '1', '1', '-1', '1', '-1' ] dicts = [dict(zip(['x', 'y', 'z'], l[i:i+3])) in range(0, len(l), 3)] result = dict(enumerate(dicts)) print result #prints {0: {'y': '1', 'x': '1', 'z': '1'}, 1: {'y': '1', 'x': '-1', 'z': '-1'}}
Comments
Post a Comment