Concatenation of a variant number of keys of a dictionary Python (recursion?) -


hello stackoverlow members,

i'm trying concatenate keys (string) on hand, , values (list) on other hand, of dictionnary.

for better understanding, here have @ beginning:

dict = {'bk1':             {'k11': ['a1', 'b1', 'c1'],              'k12': ['a2', 'b2', 'c2']},         'bk2':             {'k21': ['d1', 'e1'],              'k22': ['d2', 'e2'],              'k23': ['d3', 'e3']},         'bk3':             {'k31': ['f1', 'g1', 'h1'],              'k32': ['f2', 'g2', 'h2']}         } 

and here @ end:

newdict = {'k11_k21_k31': ['a1', 'b1', 'c1', 'd1', 'e1', 'f1', 'g1', 'h1'],            'k11_k21_k32': ['a1', 'b1', 'c1', 'd1', 'e1', 'f2', 'g2', 'h2'],            'k11_k22_k31': ['a1', 'b1', 'c1', 'd2', 'e2', 'f1', 'g1', 'h1'],            'k11_k22_k32': ['a1', 'b1', 'c1', 'd2', 'e2', 'f2', 'g2', 'h2'],            'k11_k23_k31': ['a1', 'b1', 'c1', 'd3', 'e3', 'f1', 'g1', 'h1'],            'k11_k23_k32': ['a1', 'b1', 'c1', 'd3', 'e3', 'f2', 'g2', 'h2'],            'k12_k21_k31': ['a2', 'b2', 'c2', 'd1', 'e1', 'f1', 'g1', 'h1'],            'k12_k21_k32': ['a2', 'b2', 'c2', 'd1', 'e1', 'f2', 'g2', 'h2'],            'k12_k22_k31': ['a2', 'b2', 'c2', 'd2', 'e2', 'f1', 'g1', 'h1'],            'k12_k22_k32': ['a2', 'b2', 'c2', 'd2', 'e2', 'f2', 'g2', 'h2'],            'k12_k23_k31': ['a2', 'b2', 'c2', 'd3', 'e3', 'f1', 'g1', 'h1'],            'k12_k23_k32': ['a2', 'b2', 'c2', 'd3', 'e3', 'f2', 'g2', 'h2']} 

i wish with:

  • a variant number of "big key" (bki), , each bki, variant number of key (kij).

  • "full combination" between "big keys". example, don't expect results like:

    {'k11_k23': ['a1', 'b1', 'c1', 'd3', 'e3']} 

where "bk3" missed.

i tried imbricated "for" loops number of loops depending on number of "big keys"...

then, felt problem solved recursion (maybe?), in spite of research , implement it, failed.

any "recursive or not" solution appreciated.

thank you,

mat


whoaa, reactivity! lot quick answers, works perfect!

as suggested @jksnw in comments, can use itertools.product this:

import itertools  dct = {     'bk1': {         'k11': ['a1', 'b1', 'c1'],         'k12': ['a2', 'b2', 'c2']     },     'bk2':{         'k21': ['d1', 'e1'],         'k22': ['d2', 'e2'],         'k23': ['d3', 'e3']     },     'bk3': {         'k31': ['f1', 'g1', 'h1'],         'k32': ['f2', 'g2', 'h2']     } }  big_keys = dct.keys() small_keys = (dct[big_key].keys() big_key in big_keys)  res = {} keys_from_each in itertools.product(*small_keys):     key = "_".join(keys_from_each)      value = []     big_key, small_key in zip(big_keys, keys_from_each):         value.extend(dct[big_key][small_key])      res[key] = value 

so that:

>>> res  {'k11_k21_k31': ['a1', 'b1', 'c1', 'd1', 'e1', 'f1', 'g1', 'h1'],  'k11_k21_k32': ['a1', 'b1', 'c1', 'd1', 'e1', 'f2', 'g2', 'h2'],  'k11_k22_k31': ['a1', 'b1', 'c1', 'd2', 'e2', 'f1', 'g1', 'h1'],  'k11_k22_k32': ['a1', 'b1', 'c1', 'd2', 'e2', 'f2', 'g2', 'h2'],  'k11_k23_k31': ['a1', 'b1', 'c1', 'd3', 'e3', 'f1', 'g1', 'h1'],  'k11_k23_k32': ['a1', 'b1', 'c1', 'd3', 'e3', 'f2', 'g2', 'h2'],  'k12_k21_k31': ['a2', 'b2', 'c2', 'd1', 'e1', 'f1', 'g1', 'h1'],  'k12_k21_k32': ['a2', 'b2', 'c2', 'd1', 'e1', 'f2', 'g2', 'h2'],  'k12_k22_k31': ['a2', 'b2', 'c2', 'd2', 'e2', 'f1', 'g1', 'h1'],  'k12_k22_k32': ['a2', 'b2', 'c2', 'd2', 'e2', 'f2', 'g2', 'h2'],  'k12_k23_k31': ['a2', 'b2', 'c2', 'd3', 'e3', 'f1', 'g1', 'h1'],  'k12_k23_k32': ['a2', 'b2', 'c2', 'd3', 'e3', 'f2', 'g2', 'h2']} 

here, itertools.product used list of "small keys" take each block:

>>> big_keys = dct.keys() >>> small_keys = (dct[big_key].keys() big_key in big_keys) >>> list(itertools.product(*small_keys))  [('k12', 'k22', 'k31'),  ('k12', 'k22', 'k32'),  ('k12', 'k23', 'k31'),  ('k12', 'k23', 'k32'),  ('k12', 'k21', 'k31'),  ('k12', 'k21', 'k32'),  ('k11', 'k22', 'k31'),  ('k11', 'k22', 'k32'),  ('k11', 'k23', 'k31'),  ('k11', 'k23', 'k32'),  ('k11', 'k21', 'k31'),  ('k11', 'k21', 'k32')] 

Comments

Popular posts from this blog

Payment information shows nothing in one page checkout page magento -

tcpdump - How to check if server received packet (acknowledged) -