Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- dict=
- {
- 'BAYMON:6680:2': {'blocks': [502698, 5067024], 'cnt': 2}
- 'GREYHORN:6679:2': {'blocks': [501930, 5025121], 'cnt': 2}
- 'GREYHORN:6681:2': {'blocks': [501930, 5025121], 'cnt': 2}
- 'GREYHORN:6680:2' :{'blocks': [501930, 5025121], 'cnt': 2}
- 'GREYHORN:6679:2' : {'blocks': [501930, 5025121], 'cnt': 2}
- 'BAYMON:6681:2' :{'blocks': [502698, 5067024], 'cnt': 2}
- }
- list = ['BAYMON:6680:2','GREYHORN:6679:2']
- # mapping of blocks -> key
- # will key the smallest key for each blocks
- smallest_dict = {}
- for key, val in dictio.items() :
- #convert to tuple so that we can use this as a key in dictionary
- blocks = tuple( val['blocks'] )
- if blocks not in smallest_dict :
- # if no key seen so far for these blocks
- smallest_dict[ blocks ] = key
- else :
- # possibly update if the key is smaller
- smallest_dict[ blocks ] = min( smallest_dict[blocks], key )
- return list( smallest_dict.values() )
- def smallest_key_by_blocks_v1( dictio ) :
- # mapping of blocks -> key
- # will key the smallest key for each blocks
- smallest_dict = {}
- for key, val in dictio.items() :
- #convert to tuple so that we can use this as a key in dictionary
- blocks = tuple( val['blocks'] )
- key_sofar = smallest_dict.get(blocks,key)
- smallest_dict[ blocks ] = min( key_sofar , key )
- return list( smallest_dict.values() )
- # first sort tuples of the form (key, blocks)
- # then traverse and build the dictionary
- # .setdefault method guarantees that only the first key in the
- # order will be assigned to blocks
- smallest_dict = {}
- sorted_tups = sorted( ( key, tuple(rec['blocks']))
- for key, rec in dictio.items() )
- for key, blocks in :
- smallest_dict.setdefault( blocks, key )
- return list( smallest_dict.values() )
Add Comment
Please, Sign In to add comment