• <small id='q2C0q'></small><noframes id='q2C0q'>

    • <bdo id='q2C0q'></bdo><ul id='q2C0q'></ul>

    1. <legend id='q2C0q'><style id='q2C0q'><dir id='q2C0q'><q id='q2C0q'></q></dir></style></legend>
      <i id='q2C0q'><tr id='q2C0q'><dt id='q2C0q'><q id='q2C0q'><span id='q2C0q'><b id='q2C0q'><form id='q2C0q'><ins id='q2C0q'></ins><ul id='q2C0q'></ul><sub id='q2C0q'></sub></form><legend id='q2C0q'></legend><bdo id='q2C0q'><pre id='q2C0q'><center id='q2C0q'></center></pre></bdo></b><th id='q2C0q'></th></span></q></dt></tr></i><div id='q2C0q'><tfoot id='q2C0q'></tfoot><dl id='q2C0q'><fieldset id='q2C0q'></fieldset></dl></div>
      1. <tfoot id='q2C0q'></tfoot>

      2. Python - 定位最近的时间戳

        时间:2023-09-14
        <i id='BopR5'><tr id='BopR5'><dt id='BopR5'><q id='BopR5'><span id='BopR5'><b id='BopR5'><form id='BopR5'><ins id='BopR5'></ins><ul id='BopR5'></ul><sub id='BopR5'></sub></form><legend id='BopR5'></legend><bdo id='BopR5'><pre id='BopR5'><center id='BopR5'></center></pre></bdo></b><th id='BopR5'></th></span></q></dt></tr></i><div id='BopR5'><tfoot id='BopR5'></tfoot><dl id='BopR5'><fieldset id='BopR5'></fieldset></dl></div>

        • <bdo id='BopR5'></bdo><ul id='BopR5'></ul>
            <tbody id='BopR5'></tbody>
            <legend id='BopR5'><style id='BopR5'><dir id='BopR5'><q id='BopR5'></q></dir></style></legend>
            <tfoot id='BopR5'></tfoot>
          • <small id='BopR5'></small><noframes id='BopR5'>

                  本文介绍了Python - 定位最近的时间戳的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  我有一个 Python 日期时间时间戳和一个大字典(索引),其中键是时间戳,值是我感兴趣的其他一些信息.

                  I have a Python datetime timestamp and a large dict (index) where keys are timestamps and the values are some other information I'm interested in.

                  我需要尽可能高效地在索引中找到最接近时间戳的日期时间(键).

                  I need to find the datetime (the key) in index that is closest to timestamp, as efficiently as possible.

                  目前我正在做类似的事情:

                  At the moment I'm doing something like:

                  for timestamp in timestamps:
                      closestTimestamp = min(index,key=lambda datetime : abs(timestamp - datetime))
                  

                  这可行,但耗时太长 - 我的索引字典有数百万个值,我正在搜索数千次.我对数据结构等很灵活 - 时间戳大致是连续的,所以我从第一个时间戳迭代到最后一个时间戳.同样,我加载到字典中的文本文件中的时间戳也是连续的.

                  which works, but takes too long - my index dict has millions of values, and I'm doing the search thousands of times. I'm flexible with data structures and so on - the timestamps are roughly sequential, so that I'm iterating from the first to the last timestamps. Likewise the timestamps in the text file that I load into the dict are sequential.

                  任何关于优化的想法都将不胜感激.

                  Any ideas for optimisation would be greatly appreciated.

                  推荐答案

                  没有组织字典以进行有效的接近未命中搜索.它们专为精确匹配而设计(使用 哈希表).

                  Dictionaries aren't organized for efficient near miss searches. They are designed for exact matches (using a hash table).

                  您最好维护一个单独的、可快速搜索的有序结构.

                  You may be better-off maintaining a separate, fast-searchable ordered structure.

                  一个简单的开始方法是使用 bisect 模块对于快速 O(log N) 搜索但较慢 O(n) 插入:

                  A simple way to start off is to use the bisect module for fast O(log N) searches but slower O(n) insertions:

                  def nearest(ts):
                      # Given a presorted list of timestamps:  s = sorted(index)
                      i = bisect_left(s, ts)
                      return min(s[max(0, i-1): i+2], key=lambda t: abs(ts - t))
                  

                  适用于非静态、动态更新的字典的更复杂的方法是使用 blist 它采用树结构进行快速 O(log N) 插入和查找.只有当 dict 会随着时间而改变时,你才需要这个.

                  A more sophisticated approach suitable for non-static, dynamically updated dicts, would be to use blist which employs a tree structure for fast O(log N) insertions and lookups. You only need this if the dict is going to change over time.

                  如果您想继续使用基于字典的方法,请考虑将具有附近时间戳的条目聚集在一起的 dict-of-lists:

                  If you want to stay with a dictionary based approach, consider a dict-of-lists that clusters entries with nearby timestamps:

                   def get_closest_stamp(ts):
                        'Speed-up timestamp search by looking only at entries in the same hour'
                        hour = round_to_nearest_hour(ts)
                        cluster = daydict[hour]         # return a list of entries
                        return min(cluster, key=lambda t: abs(ts - t))
                  

                  注意,对于靠近集群边界的准确结果,请在主集群和相邻集群中存储接近边界的时间戳.

                  Note, for exact results near cluster boundaries, store close-to-the-boundary timestamps in both the primary cluster and the adjacent cluster.

                  这篇关于Python - 定位最近的时间戳的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:在 Python 中合并和排序日志文件 下一篇:Python 按日期列出分组

                  相关文章

                  最新文章

                  <small id='iZ7Tb'></small><noframes id='iZ7Tb'>

                  <tfoot id='iZ7Tb'></tfoot>
                1. <i id='iZ7Tb'><tr id='iZ7Tb'><dt id='iZ7Tb'><q id='iZ7Tb'><span id='iZ7Tb'><b id='iZ7Tb'><form id='iZ7Tb'><ins id='iZ7Tb'></ins><ul id='iZ7Tb'></ul><sub id='iZ7Tb'></sub></form><legend id='iZ7Tb'></legend><bdo id='iZ7Tb'><pre id='iZ7Tb'><center id='iZ7Tb'></center></pre></bdo></b><th id='iZ7Tb'></th></span></q></dt></tr></i><div id='iZ7Tb'><tfoot id='iZ7Tb'></tfoot><dl id='iZ7Tb'><fieldset id='iZ7Tb'></fieldset></dl></div>

                    <legend id='iZ7Tb'><style id='iZ7Tb'><dir id='iZ7Tb'><q id='iZ7Tb'></q></dir></style></legend>

                    • <bdo id='iZ7Tb'></bdo><ul id='iZ7Tb'></ul>