<tfoot id='0fV8B'></tfoot>
      1. <small id='0fV8B'></small><noframes id='0fV8B'>

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

        <legend id='0fV8B'><style id='0fV8B'><dir id='0fV8B'><q id='0fV8B'></q></dir></style></legend>
          <bdo id='0fV8B'></bdo><ul id='0fV8B'></ul>

        在元组列表上使用 bisect 但仅使用第一个值进行比较

        时间:2023-08-30
        1. <small id='ifwkL'></small><noframes id='ifwkL'>

            <bdo id='ifwkL'></bdo><ul id='ifwkL'></ul>

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

                  <legend id='ifwkL'><style id='ifwkL'><dir id='ifwkL'><q id='ifwkL'></q></dir></style></legend>
                  本文介绍了在元组列表上使用 bisect 但仅使用第一个值进行比较的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我阅读了关于如何使用 的那个问题bisect 在元组列表上,我使用该信息来回答 那个问题.它有效,但我想要一个更通用的解决方案.

                  I read that question about how to use bisect on a list of tuples, and I used that information to answer that question. It works, but I'd like a more generic solution.

                  由于 bisect 不允许指定 key 函数,如果我有这个:

                  Since bisect doesn't allow to specify a key function, if I have this:

                  import bisect
                  test_array = [(1,2),(3,4),(5,6),(5,7000),(7,8),(9,10)]
                  

                  我想找到 x > 的第一项.5 对于那些 (x,y) 元组(根本不考虑 y,我目前正在这样做:

                  and I want to find the first item where x > 5 for those (x,y) tuples (not considering y at all, I'm currently doing this:

                  bisect.bisect_left(test_array,(5,10000))
                  

                  我得到了正确的结果,因为我知道没有 y 大于 10000,所以 bisect 指向我的索引 <代码>(7,8).如果我用 1000 代替,那就错了.

                  and I get the correct result because I know that no y is greater than 10000, so bisect points me to the index of (7,8). Had I put 1000 instead, it would have been wrong.

                  对于整数,我可以这样做

                  For integers, I could do

                  bisect.bisect_left(test_array,(5+1,))
                  

                  但是在一般情况下可能存在浮动,在不知道第二个元素的最大值的情况下如何做到这一点?

                  but in the general case when there may be floats, how to to that without knowing the max values of the 2nd element?

                  test_array = [(1,2),(3,4),(5.2,6),(5.2,7000),(5.3,8),(9,10)]
                  

                  我试过这个:

                  bisect.bisect_left(test_array,(min_value+sys.float_info.epsilon,))
                  

                  它没有用,但我试过这个:

                  and it didn't work, but I have tried this:

                  bisect.bisect_left(test_array,(min_value+sys.float_info.epsilon*3,))
                  

                  它奏效了.但这感觉像是一个糟糕的黑客攻击.有什么干净的解决方案吗?

                  and it worked. But it feels like a bad hack. Any clean solutions?

                  推荐答案

                  bisect 支持任意序列.如果您需要将 bisect 与密钥一起使用,而不是将密钥传递给 bisect,您可以将其构建到序列中:

                  bisect supports arbitrary sequences. If you need to use bisect with a key, instead of passing the key to bisect, you can build it into the sequence:

                  class KeyList(object):
                      # bisect doesn't accept a key function, so we build the key into our sequence.
                      def __init__(self, l, key):
                          self.l = l
                          self.key = key
                      def __len__(self):
                          return len(self.l)
                      def __getitem__(self, index):
                          return self.key(self.l[index])
                  

                  然后你可以使用 bisect 和一个 KeyList,具有 O(log n) 性能并且不需要复制 bisect 源或编写你自己的二分搜索:

                  Then you can use bisect with a KeyList, with O(log n) performance and no need to copy the bisect source or write your own binary search:

                  bisect.bisect_right(KeyList(test_array, key=lambda x: x[0]), 5)
                  

                  这篇关于在元组列表上使用 bisect 但仅使用第一个值进行比较的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:在 Python 3 中将整数的字符串表示形式编码为 base64 下一篇:这不是元组吗?

                  相关文章

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

                  <tfoot id='nu19h'></tfoot>

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

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

                    1. <legend id='nu19h'><style id='nu19h'><dir id='nu19h'><q id='nu19h'></q></dir></style></legend>