繁体   English   中英

查看python字典的字典中是否存在键列表

[英]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'}}

我想做什么?

allDatetimesdtDates字典中每个字典的键进行比较,如果一个键不存在,我想添加它并为其赋予上一个键的值。 例如,在上面的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对。 除了我现在拥有的生成dtDictallDatetimes的代码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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM