![](/img/trans.png)
[英]python : How to get the max value of dictionary keys in list of dictionaries
[英]see if list of keys exist in dictionaries of a dictionary in python
我有一個清單:
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'}}
我想做什么?
將allDatetimes
與dtDates
字典中每個字典的鍵進行比較,如果一個鍵不存在,我想添加它並為其賦予上一個鍵的值。 例如,在上面的dtDates
詞典中,對於鍵AV-IM-1-13991733
,我們可以看到缺少6/1/2014 0:15
,因此我想添加該鍵並為其提供一個等於該值的值上一個鍵,因此我想將dtDict
更改為:
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'}}`
因此,現在dtDict
所有字典dtDict
具有相同數量的key:value
對。 除了我現在擁有的生成dtDict
和allDatetimes
的代碼allDatetimes
,我還嘗試過這種方式來檢查密鑰的存在,但是它不起作用:
for meter,date in dtDict.iteritems():
if all(dateval in dtDict[meter] for dateval in allDatetimes):
print meter,dateval,volt
#then how do I "add" the missing key and give it a value of previous key?
好吧,如果我理解正確,是這樣的嗎?:
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'}}
from datetime import datetime
for k,d in dtDict.iteritems(): # .items python3
diff = allDatetimes - d.viewkeys() # .keys() python3
for 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")))]
from pprint import pprint as pp
pp(dtDict)
輸出:
{'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()
查找不在我設置的allDatetimes
的鍵,然后找到最接近的時間戳並將該鍵值設置為缺少鍵的值。
唯一的錯誤是代碼還會找到時間戳本身之后最接近的時間戳,我們可以滾動一個函數來確保僅找到時間戳之前的時間,但是請記住,如果缺少最早的時間戳關鍵是不會有更早的時間,那么您要做的就是決定:
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()
for k,d in dtDict.items():
diff = allDatetimes - d.viewkeys()
for k in diff:
dt1 = datetime.strptime(k, "%m/%d/%Y %H:%M" )
d[k] = d[min(d, key=lambda x:find_closest(dt1,x))]
就有效地完成此操作而言,如果您要大量執行此操作並建議使用OrderedDict
來使時間戳保持順序,則建議將鍵存儲為實際的datetime對象。 使用bisect
搜索或使用顯式循環查找來查找先前的時間戳/鍵只是一個問題。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.