查看python中字典的字典中是否存在键列表
我有一份清单:查看python中字典的字典中是否存在键列表,python,dictionary,Python,Dictionary,我有一份清单: 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'
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
字典中每个字典的键进行比较,如果一个键不存在,我想添加它并给它一个previous key的值。例如,在上面的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
中的所有词典都有相同数量的key:value
对。
除了我现在生成的dtDict
和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))]
为了有效地执行此操作,我建议您将密钥存储为实际的datetime对象,如果您打算经常这样做,并使用
OrderedDict
将时间戳保持有序。只需使用对分
搜索或使用显式循环进行迭代,即可找到前一个时间戳/键 dicts没有顺序,那么上一个键是什么?@PadraicCunningham最好的值是上一个时间戳值。第二个选项(如果无法获得上一个时间戳值)就是上一个键的值。你是说迭代键时出现的最后一个键吗?@padraiccnningham我想获取上一个时间戳的值。例如:如果缺少6/1/2014 0:20键,我想输入6/1/2014 0:20和6/1/2014 0:15的值,那么新的键-值对将是:'6/1/2014 0:20':'0.92'
那么前面的意思是前面的时间戳?allDatetimes应该是[]而不是{},因为它也是一个列表,如果第一个时间戳丢失,我想用“nexttime stamp”值填充它。是否可以将其添加到该代码中?另外,解决您在答案中的最后一行,如何将该代码更改为“有序字典”@NikhilGupta,它应该按原样使用下一个时间戳当时间戳最低时,使用collections.OrderedDict创建您的dicts,按排序顺序添加键我们也使用了一个集合,所以大括号是必需的。我使用了上面的代码片段,将allDatetimes定义为一个列表,它工作了。