dictionary - Dict Deconstruction and Reconstruction in Python -


i have multiple dictionaries. there great deal of overlap between dictionaries, not identical.

a = {'a':1,'b':2,'c':3} b = {'a':1,'c':3, 'd':4} c = {'a':1,'c':3} 

i'm trying figure out how break these down primitive pieces , reconstruct dictionaries in efficient manner. in other words, how can deconstruct , rebuild dictionaries typing each key/value pair minimum number of times (ideally once). means creating minimum number of sets can combined create possible sets exist.

in above example. broken down into:

c = {'a':1,'c':3} = dict(c.items() + {'b':2}) b = dict(c.items() + {'d':4}) 

i'm looking suggestions on how approach in python.

in reality, have 60 dictionaries , many of them have overlapping values. i'm trying minimize number of times have type each k/v pair minimize potential typo errors , make easier cascade update different values specific keys.

an ideal output basic dictionaries needed construct dictionaries formula reconstruction.

here solution. isn't efficient in way, might give idea of how proceed.

a = {'a':1,'b':2,'c':3} b = {'a':1,'c':3, 'd':4} c = {'a':1,'c':3}  class cover:     def __init__(self,*dicts):         # our internal representation link complete subsets, , dictionary of remaining elements         mtx = [[-1,{}] d in dicts]         i,dct in enumerate(dicts):             j,odct in enumerate(dicts):                 if == j: continue # we're subset of ourself                 # if in in b, create reference                 if all( k in dct k in odct.keys() ):                     mtx[i][0] = j                     dif = {key:value key,value in dct.items() if key not in odct}                     mtx[i][1].update(dif)                     break           i,m in enumerate(mtx):             if m[1] == {}: m[1] = dict(dicts[i].items())          self.mtx = mtx      def get(self, i):         r = { key:val key, val in self.mtx[i][1].items()}          if (self.mtx[i][0] > 0): # if had found subset, add             r.update(self.mtx[self.mtx[i][0]][1])         return r   cover = cover(a,b,c)   print(a,b,c) print('representation',cover.mtx) # prints [[2, {'b': 2}], [2, {'d': 4}], [-1, {'a': 1, 'c': 3}]] # "-1" in third element indicates building block cannot reduced; "2"s indicate these should build 2th element  print('a',cover.get(0))  print('b',cover.get(1)) print('c',cover.get(2)) 

the idea simple: if of maps complete subsets, substitute duplication reference. compression backfire matrix combinations, , can improved upon

simple improvements

change first line of get, or using more concise dictionary addition code hinted @ in question, might improve readability.

we don't check largest subset, may worthwhile.

the implementation naive , makes no optimizations

larger improvements

one implement hierarchical implementation in "building block" dictionaries formed root nodes , tree descended build larger dictionaries. beneficial if data hierarchical start.

(note: tested in python3)


Comments

Popular posts from this blog

java - pagination of xlsx file to XSSFworkbook using apache POI -

Unlimited choices in BASH case statement -

apache - How do I stop my index.php being run twice for every user -