Generating an “efficent” encoding from a list of numbers

你。 提交于 2020-06-12 08:10:44

问题


I was thinking about this the other day. Say you have the list of numbers:

1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 19, 21, 22, 23, 999, 1000, 1001

is there any library or code snipped that will turn the following into something like:

1-13, 19, 21-23, 999-1001

In other words, reduce a full list of numbers to a bunch of ranges or so. I haven't been able to find anything. If no such thing exists, anyone have some ideas for an efficient implementation?


回答1:


def get_groups(lst):
    slices = [i+1 for i, v in enumerate(zip(lst, l[1:])) if v[0] != v[1]-1] 
    slices = [0] + slices + [len(lst)]
    for start, end in zip(slices, slices[1:]):
        yield lst[start:end]

>>> list(get_groups([1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 19, 21, 22, 23, 999, 1000, 1001]))
[[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13], [19], [21, 22, 23], [999, 1000, 1001]]

or

def get_ranges(lst):
    slices = [i+1 for i, v in enumerate(zip(lst, l[1:])) if v[0] != v[1]-1]
    slices = [0] + slices + [len(lst)]
    for start, end in zip(slices, slices[1:]):
        yield "%d-%d" % (lst[start], lst[end-1])

>>> list(get_ranges([1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 19, 21, 22, 23, 999, 1000, 1001]))
['1-13', '19-19', '21-23', '999-1001']



回答2:


def compress(nums):
    nums = [int(i) for i in nums.strip().split(',')]
    answer = []
    start = nums[0]
    prev = nums[0]
    for num in nums:
        if num-prev != 1:
            answer.append("%d-%d" %(start, prev))
            start = num
        prev = num
    answer.append("%d-%d" %(start, prev))
    return answer[1:]

>>> compress("1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 19, 21, 22, 23, 999, 1000, 1001")
['1-13', '19-19', '21-23', '999-1001']

Hope this helps




回答3:


l = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 19, 21, 22, 23, 999, 1000, 1001]
range_pos = []
start=0
end=0
for i in range(1,len(l)):
    if l[i] - l[i-1] == 1:
        end = i
    else:
        range_pos.append((start, end))
        start = end = i
range_pos.append((start, end))
ranges = ["%s-%s" % (l[s], l[e]) if s < e else str(l[s]) for (s, e) in range_pos]
print ', '.join(ranges)

Giving:

1-13, 19, 21-23, 999-1001


来源:https://stackoverflow.com/questions/6128895/generating-an-efficent-encoding-from-a-list-of-numbers

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!