Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/293.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/0/search/2.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 搜索按整数时间戳排序的列表的简单方法_Python_Search - Fatal编程技术网

Python 搜索按整数时间戳排序的列表的简单方法

Python 搜索按整数时间戳排序的列表的简单方法,python,search,Python,Search,我有一个表格的日志条目列表: [{'time': 199920331000, 'message': 'message1'}, {'time': 199920331001, 'message': 'message2'}...] 其中时间值始终在列表中增加。如果我想获取晚于给定时间戳的日志,我可以遍历元素,直到看到大于给定时间戳的时间戳: def getLog(timestamp): global logs for x in range(len(logs)): if

我有一个表格的日志条目列表:

[{'time': 199920331000, 'message': 'message1'}, {'time': 199920331001, 'message': 'message2'}...]
其中时间值始终在列表中增加。如果我想获取晚于给定时间戳的日志,我可以遍历元素,直到看到大于给定时间戳的时间戳:

def getLog(timestamp):
    global logs
    for x in range(len(logs)):
        if logs[x]['time'] > timestamp:
            return logs[x:]
    return []

我想Python3中已经有了一种快速搜索机制,但不知道去哪里查找。

如果我理解正确,您正在查找,它实现了一种高效算法,用于查找排序列表中的值大于或小于给定值的点

您的日志条目需要是一个实现某种形式的排序的类。大概是这样的:

from functools import total_ordering

@total_ordering
class LogEntry(object):
    def __init__(self, time, message):
        self.time = time
        self.message = message

    def __eq__(self, other):
        if not isinstance(other, self.__class__):
            return NotImplemented
        return self.time == other.time and self.message == other.message

    def __lt__(self, other):
        if not isinstance(other, self.__class__):
            return NotImplemented
        if self.time == other.time:
            return self.message < other.message
        return self.time < other.time
def binary_search(log_list, timestamp, lo=0, hi=None):
    if hi is None:
        hi = len(log_list)
    while lo < hi:
        mid = (lo+hi)//2
        midval = log_list[mid]['time']
        if midval < timestamp:
            lo = mid+1
        elif midval > timestamp: 
            hi = mid
        else:
            return mid
    return -1

请注意,我们不需要声明
日志
全局,因为您没有分配给它。

如果您知道时间总是在增加,您可以保证您的列表已排序。 然后我会使用来自的答案并尝试调整它,如下所示:

from functools import total_ordering

@total_ordering
class LogEntry(object):
    def __init__(self, time, message):
        self.time = time
        self.message = message

    def __eq__(self, other):
        if not isinstance(other, self.__class__):
            return NotImplemented
        return self.time == other.time and self.message == other.message

    def __lt__(self, other):
        if not isinstance(other, self.__class__):
            return NotImplemented
        if self.time == other.time:
            return self.message < other.message
        return self.time < other.time
def binary_search(log_list, timestamp, lo=0, hi=None):
    if hi is None:
        hi = len(log_list)
    while lo < hi:
        mid = (lo+hi)//2
        midval = log_list[mid]['time']
        if midval < timestamp:
            lo = mid+1
        elif midval > timestamp: 
            hi = mid
        else:
            return mid
    return -1
def二进制搜索(日志列表,时间戳,lo=0,hi=None):
如果hi为无:
hi=len(日志列表)
当lo时间戳:
高=中
其他:
中途返回
返回-1

(尚未测试)

鉴于Python尝试
b.\uu gt\uuu(a)
a.\uu lt\uu(b)
未实现时,不需要更改日志条目的类,提供足够智能的密钥就足够了:

import bisect
from functools import total_ordering
from operator import itemgetter

log = [
    {'time': 199920331000, 'message': 'message1'},
    {'time': 199920331001, 'message': 'message2'},
    # ...
]

@total_ordering
class Key(object):
    def __init__(self, keyfunc, keyval):
        self.keyfunc = keyfunc
        self.keyval = keyval

    def __eq__(self, other):
        return self.keyval == self.keyfunc(other)

    def __lt__(self, other):
        return self.keyval < self.keyfunc(other)

start = bisect.bisect(log, Key(itemgetter("time"), 199920331000))
print log[start:]
(这是从中剥离出来的)