Identify groups of continuous numbers in a list

前端 未结 13 1942
误落风尘
误落风尘 2020-11-22 01:12

I\'d like to identify groups of continuous numbers in a list, so that:

myfunc([2, 3, 4, 5, 12, 13, 14, 15, 16, 17, 20])

Returns:

         


        
13条回答
  •  無奈伤痛
    2020-11-22 01:44

    A short solution that works without additional imports. It accepts any iterable, sorts unsorted inputs, and removes duplicate items:

    def ranges(nums):
        nums = sorted(set(nums))
        gaps = [[s, e] for s, e in zip(nums, nums[1:]) if s+1 < e]
        edges = iter(nums[:1] + sum(gaps, []) + nums[-1:])
        return list(zip(edges, edges))
    

    Example:

    >>> ranges([2, 3, 4, 7, 8, 9, 15])
    [(2, 4), (7, 9), (15, 15)]
    
    >>> ranges([-1, 0, 1, 2, 3, 12, 13, 15, 100])
    [(-1, 3), (12, 13), (15, 15), (100, 100)]
    
    >>> ranges(range(100))
    [(0, 99)]
    
    >>> ranges([0])
    [(0, 0)]
    
    >>> ranges([])
    []
    

    This is the same as @dansalmo's solution which I found amazing, albeit a bit hard to read and apply (as it's not given as a function).

    Note that it could easily be modified to spit out "traditional" open ranges [start, end), by e.g. altering the return statement:

        return [(s, e+1) for s, e in zip(edges, edges)]
    

    I copied this answer over from another question that was marked as a duplicate of this one with the intention to make it easier findable (after I just now searched again for this topic, finding only the question here at first and not being satisfied with the answers given).

提交回复
热议问题