• <legend id='IKITl'><style id='IKITl'><dir id='IKITl'><q id='IKITl'></q></dir></style></legend>

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

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

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

        <tfoot id='IKITl'></tfoot>

        多个范围的并集

        时间:2023-11-08

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

            <small id='30MYL'></small><noframes id='30MYL'>

            <legend id='30MYL'><style id='30MYL'><dir id='30MYL'><q id='30MYL'></q></dir></style></legend>

              • <bdo id='30MYL'></bdo><ul id='30MYL'></ul>
                  <tbody id='30MYL'></tbody>

                <tfoot id='30MYL'></tfoot>
                  本文介绍了多个范围的并集的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我有这些范围:

                  7,10
                  11,13
                  11,15
                  14,20
                  23,39
                  

                  我需要执行重叠范围的并集以给出不重叠的范围,因此在示例中:

                  I need to perform a union of the overlapping ranges to give ranges that are not overlapping, so in the example:

                  7,20
                  23,39
                  

                  我已经在 Ruby 中完成了这项工作,我在数组中推送了范围的开始和结束并对它们进行排序,然后执行重叠范围的联合.在 Python 中有什么快速的方法吗?

                  I've done this in Ruby where I have pushed the start and end of the range in array and sorted them and then perform union of the overlapping ranges. Any quick way of doing this in Python?

                  推荐答案

                  比方说,(7, 10)(11, 13) 结果为 (7, 13):

                  Let's say, (7, 10) and (11, 13) result into (7, 13):

                  a = [(7, 10), (11, 13), (11, 15), (14, 20), (23, 39)]
                  b = []
                  for begin,end in sorted(a):
                      if b and b[-1][1] >= begin - 1:
                          b[-1] = (b[-1][0], end)
                      else:
                          b.append((begin, end))
                  

                  b 现在是

                  [(7, 20), (23, 39)]
                  

                  编辑:

                  正如@CentAu 正确注意到的那样, [(2,4), (1,6)] 将返回 (1,4) 而不是 (1,6).这是正确处理这种情况的新版本:

                  As @CentAu correctly notices, [(2,4), (1,6)] would return (1,4) instead of (1,6). Here is the new version with correct handling of this case:

                  a = [(7, 10), (11, 13), (11, 15), (14, 20), (23, 39)]
                  b = []
                  for begin,end in sorted(a):
                      if b and b[-1][1] >= begin - 1:
                          b[-1][1] = max(b[-1][1], end)
                      else:
                          b.append([begin, end])
                  

                  这篇关于多个范围的并集的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                    <bdo id='7GTEW'></bdo><ul id='7GTEW'></ul>
                    <tfoot id='7GTEW'></tfoot>

                      1. <small id='7GTEW'></small><noframes id='7GTEW'>

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