code_junkie

How to do this - python dictionary traverse and search

Nov 13th, 2011
118
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.98 KB | None | 0 0
  1. {'key0': {'attrs': {'entity': 'p', 'hash': '34nj3h43b4n3', 'id': '4130'},
  2. u'key1': {'attrs': {'entity': 'r',
  3. 'hash': '34njasd3h43b4n3',
  4. 'id': '4130-1'},
  5. u'key2': {'attrs': {'entity': 'c',
  6. 'hash': '34njasd3h43bdsfsd4n3',
  7. 'id': '4130-1-1'}}},
  8. u'key3': {'attrs': {'entity': 'r',
  9. 'hash': '34njasasasd3h43b4n3',
  10. 'id': '4130-2'},
  11. u'key4': {'attrs': {'entity': 'c',
  12. 'hash': '34njawersd3h43bdsfsd4n3',
  13. 'id': '4130-2-1'}},
  14. u'key5': {'attrs': {'entity': 'c',
  15. 'hash': '34njawersd3h43bdsfsd4n3',
  16. 'id': '4130-2-2'}}}},
  17. 'someohterthing': 'someothervalue',
  18. 'something': 'somevalue'}
  19.  
  20. def walkDict( aDict, visitor, path=() ):
  21. for k in aDict:
  22. if k == 'attrs':
  23. visitor( path, aDict[k] )
  24. elif type(aDict[k]) != dict:
  25. pass
  26. else:
  27. walkDict( aDict[k], visitor, path+(k,) )
  28.  
  29. def printMe( path, element ):
  30. print path, element
  31.  
  32. def filterFor( path, element ):
  33. if element['id'] == '4130-2-2':
  34. print path, element
  35.  
  36. walkDict( myDict, filterFor )
  37.  
  38. for path, attrDict in walkDictIter( aDict ):
  39. # process attrDict...
  40.  
  41. class ProperObject( object ):
  42. """A proper class definition for each "attr" dictionary."""
  43. def __init__( self, path, attrDict ):
  44. self.path= path
  45. self.__dict__.update( attrDict )
  46. def __str__( self ):
  47. return "path %r, entity %r, hash %r, id %r" % (
  48. self.path, self.entity, self.hash, self.id )
  49.  
  50. masterDict= {}
  51. def builder( path, element ):
  52. masterDict[path]= ProperObject( path, element )
  53.  
  54. # Use the Visitor to build ProperObjects for each "attr"
  55. walkDict( myDict, builder )
  56.  
  57. # Now that we have a simple dictionary of Proper Objects, things are simple
  58. for k,v in masterDict.items():
  59. if v.id == '4130-2-2':
  60. print v
  61.  
  62. # Create an "index" of your ProperObjects
  63. import collections
  64. byId= collections.defaultdict(list)
  65. for k in masterDict:
  66. byId[masterDict[k].id].append( masterDict[k] )
  67.  
  68. # Look up a particular item in the index
  69. print map( str, byId['4130-2-2'] )
  70.  
  71. def traverse_tree(dictionary, id=None):
  72. for key, value in dictionary.items():
  73. if key == 'id':
  74. if value == id:
  75. print dictionary
  76. else:
  77. traverse_tree(value, id)
  78. return
  79.  
  80. >>> traverse_tree({1: {'id': 2}, 2: {'id': 3}}, id=2)
  81. {'id': 2}
  82.  
  83. d = { u'somekey' : 'somevalue',
  84. u'someotherkey' : 'someothervalue',
  85. u'key1' : {
  86. 'attrs' : {
  87. 'entity' : 'Post',
  88. 'hash' : '63e1ca180b86850ce51a6eb13bd2654a',
  89. 'id' : '4130'
  90. },
  91. },
  92. u'key2' : {
  93. 'attrs' : {
  94. 'entity' : 'Rev',
  95. 'hash' : '34176d03995d23251a8ce2c45e11f4ae',
  96. 'id' : '4130-1'
  97. },
  98. },
  99. u'key3' : {
  100. 'attrs' : {
  101. 'entity' : 'Comment',
  102. 'hash' : '34176d03995d23251a8ce2c45e11f4er',
  103. 'id' : '4130-1-1'
  104. },
  105. },
  106. u'key4' : {
  107. 'attrs' : {
  108. 'entity' : 'Rev',
  109. 'hash' : '34176d03995d23251a8ce2c45e11f4qe',
  110. 'id' : '4130-2'
  111. },
  112. },
  113. u'key5' : {
  114. 'attrs' : {
  115. 'entity' : 'Comment',
  116. 'hash' : '34176d03995d23251a8ce2c45e11f4er',
  117. 'id' : '4130-2-1'
  118. },
  119. },
  120. u'key6' : {
  121. 'attrs' : {
  122. 'entity' : 'Comment',
  123. 'hash' : '34176d03995d23251a8ce2c4er1f4er',
  124. 'id' : '4130-2-2'
  125. },
  126. },
  127. }
Add Comment
Please, Sign In to add comment