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

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

    <tfoot id='N0SC1'></tfoot>

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

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

        如何进行逆“范围",即根据一组数字创建一个紧凑的范围?

        How to do an inverse `range`, i.e. create a compact range based on a set of numbers?(如何进行逆“范围,即根据一组数字创建一个紧凑的范围?)
          <i id='0q9KR'><tr id='0q9KR'><dt id='0q9KR'><q id='0q9KR'><span id='0q9KR'><b id='0q9KR'><form id='0q9KR'><ins id='0q9KR'></ins><ul id='0q9KR'></ul><sub id='0q9KR'></sub></form><legend id='0q9KR'></legend><bdo id='0q9KR'><pre id='0q9KR'><center id='0q9KR'></center></pre></bdo></b><th id='0q9KR'></th></span></q></dt></tr></i><div id='0q9KR'><tfoot id='0q9KR'></tfoot><dl id='0q9KR'><fieldset id='0q9KR'></fieldset></dl></div>
        • <small id='0q9KR'></small><noframes id='0q9KR'>

          <tfoot id='0q9KR'></tfoot>
              <tbody id='0q9KR'></tbody>
            <legend id='0q9KR'><style id='0q9KR'><dir id='0q9KR'><q id='0q9KR'></q></dir></style></legend>

                <bdo id='0q9KR'></bdo><ul id='0q9KR'></ul>

                  本文介绍了如何进行逆“范围",即根据一组数字创建一个紧凑的范围?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  Python 有一个 range 方法,它允许使用以下内容:

                  Python has a range method, which allows for stuff like:

                  >>> range(1, 6)
                  [1, 2, 3, 4, 5]
                  

                  我正在寻找的是相反的:获取一个数字列表,然后返回开始和结束.

                  What I’m looking for is kind of the opposite: take a list of numbers, and return the start and end.

                  >>> magic([1, 2, 3, 4, 5])
                  [1, 5] # note: 5, not 6; this differs from `range()`
                  

                  对于上面的示例来说,这很容易做到,但是是否也可以允许间隙或多个范围,以类似 PCRE 的字符串格式返回范围? 像这样:

                  This is easy enough to do for the above example, but is it possible to allow for gaps or multiple ranges as well, returning the range in a PCRE-like string format? Something like this:

                  >>> magic([1, 2, 4, 5])
                  ['1-2', '4-5']
                  >>> magic([1, 2, 3, 4, 5])
                  ['1-5']
                  

                  我正在寻找 Python 解决方案,但我也欢迎使用其他语言的工作示例.它更多地是关于找出一个优雅、高效的算法.额外的问题:是否有任何编程语言对此具有内置方法?

                  I’m looking for a Python solution, but I welcome working examples in other languages as well. It’s more about figuring out an elegant, efficient algorithm. Bonus question: is there any programming language that has a built-in method for this?

                  推荐答案

                  简化代码的一个好技巧是查看排序列表的每个元素及其索引的差异:

                  A nice trick to simplify the code is to look at the difference of each element of the sorted list and its index:

                  a = [4, 2, 1, 5]
                  a.sort()
                  print [x - i for i, x in enumerate(a)]
                  

                  打印

                  [1, 1, 2, 2]
                  

                  每个相同数字的运行对应于 a 中的一系列连续数字.我们现在可以使用 itertools.groupby() 来提取这些运行.这是完整的代码:

                  Each run of the same number corresponds to a run of consecutive numbers in a. We can now use itertools.groupby() to extract these runs. Here's the complete code:

                  from itertools import groupby
                  
                  def sub(x):
                      return x[1] - x[0]
                  
                  a = [5, 3, 7, 4, 1, 2, 9, 10]
                  ranges = []
                  for k, iterable in groupby(enumerate(sorted(a)), sub):
                       rng = list(iterable)
                       if len(rng) == 1:
                           s = str(rng[0][1])
                       else:
                           s = "%s-%s" % (rng[0][1], rng[-1][1])
                       ranges.append(s)
                  print ranges
                  

                  打印

                  ['1-5', '7', '9-10']
                  

                  这篇关于如何进行逆“范围",即根据一组数字创建一个紧凑的范围?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  What happens when you compare 2 pandas Series(当你比较 2 个 pandas 系列时会发生什么)
                  Quickly find differences between two large text files(快速查找两个大文本文件之间的差异)
                  Python - Compare 2 files and output differences(Python - 比较 2 个文件和输出差异)
                  Why do comparisions between very large float values fail in python?(为什么在 python 中非常大的浮点值之间的比较会失败?)
                  Dictionary merge by updating but not overwriting if value exists(字典通过更新合并,但如果值存在则不覆盖)
                  Find entries of one text file in another file in python(在python中的另一个文件中查找一个文本文件的条目)

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

                      <tbody id='ta64i'></tbody>
                      <bdo id='ta64i'></bdo><ul id='ta64i'></ul>
                        <tfoot id='ta64i'></tfoot>

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

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