我仅通过使用 log.append(line) 在当前包含最后 1000000 个条目的列表中维护日志。为了确保当大小达到 2000000 时它不会变得太长,我复制了 do log = log[1000000:] 。然而,这相当慢。
在 C 中,我可以使用链表将指针移动到日志中间的位置。然而,这不是一个很好的解决方案,因为我无法再快速跳转到日志中的特定条目。
是否有 python 解决方案允许我在任何地方截断日志,将内容添加到日志末尾但仍允许快速访问 log[i] ?
最佳答案
您可以使用 collections.deque
:
Deques support thread-safe, memory efficient appends and pops from either side of the deque with approximately the same O(1) performance in either direction
对于py2.6之前的python版本:
追加长度检查时,如果长度大于 1000000,则执行 popleft
删除最左边的项目,这样列表将始终包含最后一个 1000000
项。
如果你的 python 版本是 py2.6+ 那么简单地利用 maxlen
参数:
If maxlen is not specified or is None, deques may grow to an arbitrary length. Otherwise, the deque is bounded to the specified maximum length. Once a bounded length deque is full, when new items are added, a corresponding number of items are discarded from the opposite end. Bounded length deques provide functionality similar to the tail filter in Unix. They are also useful for tracking transactions and other pools of data where only the most recent activity is of interest.
关于可截断循环缓冲区的Python快速数据结构,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17875997/