Guest User

Untitled

a guest
Dec 12th, 2018
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.95 KB | None | 0 0
  1. dict=
  2. {
  3. 'BAYMON:6680:2': {'blocks': [502698, 5067024], 'cnt': 2}
  4. 'GREYHORN:6679:2': {'blocks': [501930, 5025121], 'cnt': 2}
  5. 'GREYHORN:6681:2': {'blocks': [501930, 5025121], 'cnt': 2}
  6. 'GREYHORN:6680:2' :{'blocks': [501930, 5025121], 'cnt': 2}
  7. 'GREYHORN:6679:2' : {'blocks': [501930, 5025121], 'cnt': 2}
  8. 'BAYMON:6681:2' :{'blocks': [502698, 5067024], 'cnt': 2}
  9. }
  10.  
  11. list = ['BAYMON:6680:2','GREYHORN:6679:2']
  12.  
  13. # mapping of blocks -> key
  14. # will key the smallest key for each blocks
  15. smallest_dict = {}
  16.  
  17. for key, val in dictio.items() :
  18. #convert to tuple so that we can use this as a key in dictionary
  19. blocks = tuple( val['blocks'] )
  20.  
  21. if blocks not in smallest_dict :
  22. # if no key seen so far for these blocks
  23. smallest_dict[ blocks ] = key
  24. else :
  25. # possibly update if the key is smaller
  26. smallest_dict[ blocks ] = min( smallest_dict[blocks], key )
  27.  
  28. return list( smallest_dict.values() )
  29.  
  30. def smallest_key_by_blocks_v1( dictio ) :
  31.  
  32. # mapping of blocks -> key
  33. # will key the smallest key for each blocks
  34. smallest_dict = {}
  35. for key, val in dictio.items() :
  36. #convert to tuple so that we can use this as a key in dictionary
  37. blocks = tuple( val['blocks'] )
  38. key_sofar = smallest_dict.get(blocks,key)
  39. smallest_dict[ blocks ] = min( key_sofar , key )
  40.  
  41. return list( smallest_dict.values() )
  42.  
  43. # first sort tuples of the form (key, blocks)
  44. # then traverse and build the dictionary
  45. # .setdefault method guarantees that only the first key in the
  46. # order will be assigned to blocks
  47. smallest_dict = {}
  48. sorted_tups = sorted( ( key, tuple(rec['blocks']))
  49. for key, rec in dictio.items() )
  50. for key, blocks in :
  51. smallest_dict.setdefault( blocks, key )
  52.  
  53. return list( smallest_dict.values() )
Add Comment
Please, Sign In to add comment