我有这样的字典。
<form target="form-iframe" method="POST">
<input name="name" type="text"/>
<input name="email" type="text"/>
<input value="Submit" type="submit"/>
</form>
<iframe name="form-iframe" src=""></iframe>
我想检查
如果字符串var(说)与键'name'的值匹配
如果类型为date的变量x(比如)与键'date'的值匹配
在词典data1中的词典列表中。
答案 0 :(得分:0)
因为我不知道你想要达到的确切格式,所以我必须做一些相当明显的猜测工作。无论如何,我希望这会有所帮助。祝你好运
import datetime
data1 = {"00:08:22:24:f8:02":
{"cities" :
[{'name': 'Bhubaneswar', 'count': 12, 'date':'14/05/2016'},
{'name': 'Kolkata', 'count': 4, 'date': '15/05/2016'},
{'name': 'Mumbai', 'count': 6, 'date' : '16/04/2016'}]
}
}
#first issue with your origonal data, cities would actually become a dict based on your origonal data
def get_name(dict, name, hash=None, position=None):
'''
:arg dict is your data set
:arg name is the name you want to check for
:arg hash is the first index of your dict
:arg position is the 2nd index of your dict
:returns return the first object that contains name
'''
if name and hash and position:
for i, v in enumerate(dict[hash][position]):
try:
if name in v['name']:
return dict[hash][position][i]
except:
pass
else:
for hash, v in dict.iteritems():
for position, v2 in v.iteritems():
for i, index in enumerate(v2):
for field, v3 in index.iteritems():
if name in str(v3):
return index
return {}
def is_date_greater(dict, date):
'''
:arg dict is your data set
:arg date is the date you want to compare with
:returns True if date is greater than the supplied date
'''
date = datetime.datetime.strptime(date, '%d/%m/%y')
try:
n_date = datetime.datetime.strptime(dict['date'], '%d/%m/%Y')
if date < n_date:
return True
except:
pass
return False
print is_date_greater(get_name(data1, 'Bhub'), '12/4/16')