Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/list/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 3.x python字典列表上的二进制搜索_Python 3.x_List_Dictionary_Binary Search - Fatal编程技术网

Python 3.x python字典列表上的二进制搜索

Python 3.x python字典列表上的二进制搜索,python-3.x,list,dictionary,binary-search,Python 3.x,List,Dictionary,Binary Search,我正在用python编写一个二进制搜索函数来查找字典列表中的元素。我的基本功能正常工作,但我不知道如何调整它以在字典列表中搜索。下面是我的数据集(它要大得多)和我开发的函数的示例。如前所述,该函数用于简单的数字列表,但不用于数据集。我想在此实例中搜索“datetime”(例如,2019年10月2日发生的所有条目)。我还希望能够在字典列表中搜索一系列值,即“surface_temp”在65到100之间的条目 fire1 = [{'latitude': '-37.591', 'longitude'

我正在用python编写一个二进制搜索函数来查找字典列表中的元素。我的基本功能正常工作,但我不知道如何调整它以在字典列表中搜索。下面是我的数据集(它要大得多)和我开发的函数的示例。如前所述,该函数用于简单的数字列表,但不用于数据集。我想在此实例中搜索“datetime”(例如,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


TypeError:“对于您的
TypeError
,将
key
替换为
key
。如何将字符串与字典进行比较?这也是你的错误告诉你的。至于二进制搜索,在使用函数之前,根据要搜索的值对列表进行排序。如果要基于
表面温度进行搜索
请使用
fire1.sort(key=lambda row:row['surface\u temp'])
。感谢您的回复,我如何编写它以便搜索温度范围?比如说在50到70之间?我已经尝试了这个修复,虽然它修复了错误,但它返回一个空列表,它不应该返回空列表,因为在queryInclude下肯定有条目,包括您尝试过的内容。不要像在二进制搜索中那样查找元素,而是在范围与要查找的范围相交时停止。@SurajSubramanian我找到了,谢谢。
binary_search(fire1, '2019-10-02')