see if list of keys exist in dictionaries of a dictionary in python -


i have list:

alldatetimes = ['6/1/2014 0:10', '6/1/2014 0:20', '6/1/2014 0:15'] 

and dictionary:

dtdict={'av-im-1-13991730': {'6/1/2014 0:10': '0.96', '6/1/2014 0:20': '0.97', '6/1/2014 0:15': '0.92'}, 'av-im-1-13991731': {'6/1/2014 0:10': '1.96', '6/1/2014 0:20': '1.97', '6/1/2014 0:15': '1.92'}, 'av-im-1-13991732': {'6/1/2014 0:10': '2.96', '6/1/2014 0:20': '2.97', '6/1/2014 0:15': '2.92'}, 'av-im-1-13991733': {'6/1/2014 0:20': '3.97', '6/1/2014 0:10': '3.96'}} 

what trying do?

compare alldatetimes keys of each dictionary in dtdates dictionary , if key doesn't exist, want add , give value of previous key. example, in above dtdates dictionary, key av-im-1-13991733, can see 6/1/2014 0:15 missing, want add key , give value equal value of previous key, want change dtdict to:

dtdict={'av-im-1-13991730': {'6/1/2014 0:10': '0.96', '6/1/2014 0:20': '0.97', '6/1/2014 0:15': '0.92'}, 'av-im-1-13991731': {'6/1/2014 0:10': '1.96', '6/1/2014 0:20': '1.97', '6/1/2014 0:15': '1.92'}, 'av-im-1-13991732': {'6/1/2014 0:10': '2.96', '6/1/2014 0:20': '2.97', '6/1/2014 0:15': '2.92'}, 'av-im-1-13991733': {'6/1/2014 0:20': '3.97', '6/1/2014 0:10': '3.96', '6/1/2014 0:15': '3.96'}}` 

so dictionaries in dtdict has same number of key:value pairs. in addition code have right generate dtdict , alldatetimes, tried way check existence of keys not working:

for meter,date in dtdict.iteritems():     if all(dateval in dtdict[meter] dateval in alldatetimes):         print meter,dateval,volt         #then how "add" missing key , give value of previous key? 

ok if understand correctly, this?:

alldatetimes = {'6/1/2014 0:10', '6/1/2014 0:20', '6/1/2014 0:15'}  dtdict = {'av-im-1-13991730': {'6/1/2014 0:10': '0.96', '6/1/2014 0:20': '0.97', '6/1/2014 0:15': '0.92'}, 'av-im-1-13991731': {'6/1/2014 0:10': '1.96', '6/1/2014 0:20': '1.97', '6/1/2014 0:15': '1.92'}, 'av-im-1-13991732': {'6/1/2014 0:10': '2.96', '6/1/2014 0:20': '2.97', '6/1/2014 0:15': '2.92'}, 'av-im-1-13991733': {'6/1/2014 0:20': '3.97', '6/1/2014 0:10': '3.96'}} datetime import datetime k,d in dtdict.iteritems(): # .items python3     diff = alldatetimes - d.viewkeys() # .keys() python3     k in diff:         dt1 = datetime.strptime(k, "%m/%d/%y %h:%m" )         d[k] = d[min(d,key=lambda x: abs(dt1 - datetime.strptime(x,"%m/%d/%y %h:%m")))] pprint import  pprint pp pp(dtdict) 

output:

{'av-im-1-13991730': {'6/1/2014 0:10': '0.96',                       '6/1/2014 0:15': '0.92',                       '6/1/2014 0:20': '0.97'},  'av-im-1-13991731': {'6/1/2014 0:10': '1.96',                       '6/1/2014 0:15': '1.92',                       '6/1/2014 0:20': '1.97'},  'av-im-1-13991732': {'6/1/2014 0:10': '2.96',                       '6/1/2014 0:15': '2.92',                       '6/1/2014 0:20': '2.97'},  'av-im-1-13991733': {'6/1/2014 0:10': '3.96',                       '6/1/2014 0:15': '3.96',                       '6/1/2014 0:20': '3.97'}} 

diff = alldatetimes - d.viewkeys() finds keys not in alldatetimes made set, find closest timestamp , set keys value value missing key.

the bug code find closest timestamp after timestamp itself, can roll function make sure find times before our timestamp bear in mind if have earliest timestamp missing key there no earlier time, have decide:

def find_closest(dt,x):     dt2 = datetime.strptime(x,"%m/%d/%y %h:%m")     return abs(dt2 - dt) if dt > dt2 else dt2 - datetime.today()  k,d in dtdict.items():     diff = alldatetimes - d.viewkeys()     k in diff:         dt1 = datetime.strptime(k, "%m/%d/%y %h:%m" )         d[k] = d[min(d, key=lambda x:find_closest(dt1,x))] 

as far doing efficiently recommend storing keys actual datetime objects if going doing lot , using ordereddict keeping timestamps in order. matter of using bisect search or iterating in using explicit loop find find previous timestamp/key.


Comments