如何解决在字典python
我正在用python编写二进制搜索函数,以查找在词典列表中的元素。我具有基本功能,但不确定如何调整它以在词典列表中进行搜索。以下是我的数据集(它要大得多)和我开发的功能的示例。如前所述,该函数仅适用于简单的数字列表,但不适用于数据集。我想在这种情况下搜索``日期时间''(例如,所有发生在2019年10月2日的条目)。我也希望能够在词典列表中搜索一系列值。 “ surface_temp”介于65和100之间的条目。
fire1 = [{'latitude': '-37.591','longitude': '148.111','datetime':'2019-10-02T03:52:12','surface_temp':'57'},{'latitude': '-31.231','longitude': '328.314','datetime':'2017-11-10T06:42:17','surface_temp':'60'},'latitude': '-23.594','longitude': '132.435','datetime':'2017-08-15T03:52:12','surface_temp':'23'}]
# Binary search function
def binary_search(data,key):
matched_record = []
bottom = 0
top = len(data) -1
found = False
while(bottom <= top):
middle = int((bottom + top)//2)
if data[middle] == key :
matched_record.append((middle,key)) # position,key
# looking for the duplicates in left side of the match
for i in range(middle - 1,-1,-1):
if data[i] == key:
matched_record.append((i,key))
else:
break
# looking for the duplicates in right side of the match
for i in range(middle + 1,top):
if data[i] == key:
matched_record.append((i,key))
else:
break
break
else:
if key < data[middle]:
top = middle -1
else:
bottom = middle +1
return matched_record
当我尝试使用数据集运行代码时,出现以下错误:
binary_search(fire1,'2019-10-02')
TypeError:“ str”和“ dict”的实例之间不支持“
非常感谢您提供任何帮助或指导,如果需要添加更多信息,请告诉我。
预先感谢
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。