I have a nested list of tuples as given below,
>>> my_nested_list_of_tuples
[[('AU041133( )', 'Qtrt1'), ('Ahr( )', 'Mex3c'), ('Arid3a( )', 'Hmgb1')],
[('AU041133( )', 'Topors'), ('Ahr( )', 'Mex3c'), ('Arid3a( )', 'Hmgb1')],
[('AU041133( )', 'Tm9sf3'), ('Ahr( )', 'Mex3c'), ('Arid3a( )', 'Hmgb1')],
[('AU041133( )', 'Gsn'), ('Ahr( )', 'Ireb2'), ('Arid3a( )', 'Hmgb1')],
[('AU041133( )', 'Tm9sf3'), ('Ahr( )', 'Ilf3'), ('Arid3a( )', 'Arid3a')]]
I want to convert the above nested list of tuples to list of dictionaries with whole tuple as key and 0 as value as given below
[{('AU041133( )', 'Qtrt1'): 0, ('Ahr( )', 'Mex3c'): 0, ('Arid3a( )', 'Hmgb1'): 0},
{('AU041133( )', 'Topors'): 0, ('Ahr( )', 'Mex3c'): 0, ('Arid3a( )', 'Hmgb1'): 0},
{('AU041133( )', 'Tm9sf3'): 0, ('Ahr( )', 'Mex3c'): 0, ('Arid3a( )', 'Hmgb1'): 0},
{('AU041133( )', 'Gsn'): 0, ('Ahr( )', 'Ireb2'): 0, ('Arid3a( )', 'Hmgb1'): 0},
{('AU041133( )', 'Tm9sf3'): 0, ('Ahr( )', 'Ilf3'): 0, ('Arid3a( )', 'Arid3a'): 0}]
I have tried various methods for achieving this but I am not successful in doing so, one of the way I tried is given below,
>>> d = defaultdict(list)
>>> dk=[] # for populating with list of dicts
>>> for k in my_tops:
... for i,v in k:
... d[(i,v)].append(0)
... dk.append(d)
...
>>> dk
[defaultdict(<class 'list'>, {('AU041133( )', 'Qtrt1'): [0], ('Ahr( )', 'Mex3c'): [0, 0, 0],
('Arid3a( )', 'Hmgb1'): [0, 0, 0, 0], ('AU041133( )', 'Topors'): [0], ('AU041133( )', 'Tm9sf3'): [0, 0],
('AU041133( )', 'Gsn'): [0], ('Ahr( )', 'Ireb2'): [0], ('Ahr( )', 'Ilf3'): [0], ('Arid3a( )', 'Arid3a'): [0]}),
defaultdict(<class 'list'>, {('AU041133( )', 'Qtrt1'): [0], ('Ahr( )', 'Mex3c'): [0, 0, 0],
('Arid3a( )', 'Hmgb1'): [0, 0, 0, 0], ('AU041133( )', 'Topors'): [0], ('AU041133( )', 'Tm9sf3'): [0, 0],
('AU041133( )', 'Gsn'): [0], ('Ahr( )', 'Ireb2'): [0], ('Ahr( )', 'Ilf3'): [0], ('Arid3a( )', 'Arid3a'): [0]}),
defaultdict(<class 'list'>, {('AU041133( )', 'Qtrt1'): [0], ('Ahr( )', 'Mex3c'): [0, 0, 0],
('Arid3a( )', 'Hmgb1'): [0, 0, 0, 0], ('AU041133( )', 'Topors'): [0], ('AU041133( )', 'Tm9sf3'): [0, 0],
('AU041133( )', 'Gsn'): [0], ('Ahr( )', 'Ireb2'): [0], ('Ahr( )', 'Ilf3'): [0], ('Arid3a( )', 'Arid3a'): [0]}),
defaultdict(<class 'list'>, {('AU041133( )', 'Qtrt1'): [0], ('Ahr( )', 'Mex3c'): [0, 0, 0],
('Arid3a( )', 'Hmgb1'): [0, 0, 0, 0], ('AU041133( )', 'Topors'): [0], ('AU041133( )', 'Tm9sf3'): [0, 0],
('AU041133( )', 'Gsn'): [0], ('Ahr( )', 'Ireb2'): [0], ('Ahr( )', 'Ilf3'): [0], ('Arid3a( )', 'Arid3a'): [0]}),
defaultdict(<class 'list'>, {('AU041133( )', 'Qtrt1'): [0], ('Ahr( )', 'Mex3c'): [0, 0, 0], ('Arid3a( )', 'Hmgb1'): [0, 0, 0, 0],
('AU041133( )', 'Topors'): [0], ('AU041133( )', 'Tm9sf3'): [0, 0],
('AU041133( )', 'Gsn'): [0], ('Ahr( )', 'Ireb2'): [0], ('Ahr( )', 'Ilf3'): [0], ('Arid3a( )', 'Arid3a'): [0]})]
Is there a way to get the expected output as given in the second chunk of code?
Thanks,
CodePudding user response:
If I understood correctly your question, the solution could be:
dict_list=[]
>>> for row in my_nested_list_of_tuples:
... dict_list.append({key:0 for key in row})
[{('AU041133( )', 'Qtrt1'): 0, ('Ahr( )', 'Mex3c'): 0, ('Arid3a( )', 'Hmgb1'): 0},
{('AU041133( )', 'Topors'): 0, ('Ahr( )', 'Mex3c'): 0, ('Arid3a( )', 'Hmgb1'): 0},
{('AU041133( )', 'Tm9sf3'): 0, ('Ahr( )', 'Mex3c'): 0, ('Arid3a( )', 'Hmgb1'): 0},
{('AU041133( )', 'Gsn'): 0, ('Ahr( )', 'Ireb2'): 0, ('Arid3a( )', 'Hmgb1'): 0},
{('AU041133( )', 'Tm9sf3'): 0, ('Ahr( )', 'Ilf3'): 0, ('Arid3a( )', 'Arid3a'): 0}]