查找 Python 集合中的最小连续整数

发布于 2024-10-06 14:42:23 字数 346 浏览 0 评论 0原文

获取 Python 集合中最小 N 个连续整数的列表的最佳方法是什么?

>>> s=set([5,6,10,12,13,15,30,40,41,42,43,44,55,56,90,300,500])
>>> s
set([42, 43, 44, 5, 6, 90, 300, 30, 10, 12, 13, 55, 56, 15, 500, 40, 41])
>>> smallest_contiguous(s,5)
[40,41,42,43,44]
>>> smallest_contiguous(s,6)
[]

编辑:谢谢大家的回答。

What is the best way to get a list of the smallest N contiguous integers in a Python set?

>>> s=set([5,6,10,12,13,15,30,40,41,42,43,44,55,56,90,300,500])
>>> s
set([42, 43, 44, 5, 6, 90, 300, 30, 10, 12, 13, 55, 56, 15, 500, 40, 41])
>>> smallest_contiguous(s,5)
[40,41,42,43,44]
>>> smallest_contiguous(s,6)
[]

Edit: Thanks for the answers, everyone.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

笔芯 2024-10-13 14:42:23

斯文的想法是正确的。您只需检查前面的数字 N - 1 即可避免检查超集。

def smallest_contiguous(s, N):
    lst = list(s)
    lst.sort()
    Nm = N-1
    for i in xrange(len(lst) - Nm):
        if lst[i] + Nm == lst[i + Nm]:
            return range(lst[i], lst[i]+N)
    return []

这仅对于作为输入的集合并且知道该集合仅包含整数而言始终是正确的。

Sven has the right idea. You can avoid having to check supersets by just checking the number N - 1 ahead.

def smallest_contiguous(s, N):
    lst = list(s)
    lst.sort()
    Nm = N-1
    for i in xrange(len(lst) - Nm):
        if lst[i] + Nm == lst[i + Nm]:
            return range(lst[i], lst[i]+N)
    return []

This will only always be correct for a set as input and knowing that the set only contains integers.

野鹿林 2024-10-13 14:42:23

这个怎么样?

def smallest_contiguous(s, N):
    lst = sorted(s)
    for i in lst:
        t = range(i, i+N)
        if s.issuperset(t):
            return t
    return []

它可能不是最有效的解决方案,但它很简洁。

编辑:贾斯汀的方法也可以变得更简洁:

def smallest_contiguous(s, N):
    lst = sorted(s)
    for a, b in zip(lst, lst[N - 1:]):
        if b - a == N - 1:
            return range(a, b + 1)
    return []

How about this?

def smallest_contiguous(s, N):
    lst = sorted(s)
    for i in lst:
        t = range(i, i+N)
        if s.issuperset(t):
            return t
    return []

It might not be the most efficient solution, but it is concise.

Edit: Justin's approach could also be made more concise:

def smallest_contiguous(s, N):
    lst = sorted(s)
    for a, b in zip(lst, lst[N - 1:]):
        if b - a == N - 1:
            return range(a, b + 1)
    return []
时光倒影 2024-10-13 14:42:23

应该可以了...在排序列表中向前查看 length - 1 步。由于它仅包含整数并且已排序,因此差值也必须为 length - 1

def smallest_contiguous(myset, length):
    if len(myset) < length:
        return []

    s = sorted(myset)
    for idx in range(0, len(myset) - length + 1):
        if s[idx+length-1] - s[idx] == length - 1:
            return s[idx:idx+length]

    return []

s=set([5,6,10,12,13,15,30,40,41,42,43,44,55,56,90,300,500])
print smallest_contiguous(s, 5)
print smallest_contiguous(s, 6)

That should do it ... look ahead length - 1 steps in the sorted list. Since it contains integers only and is sorted, the difference must be length - 1 as well.

def smallest_contiguous(myset, length):
    if len(myset) < length:
        return []

    s = sorted(myset)
    for idx in range(0, len(myset) - length + 1):
        if s[idx+length-1] - s[idx] == length - 1:
            return s[idx:idx+length]

    return []

s=set([5,6,10,12,13,15,30,40,41,42,43,44,55,56,90,300,500])
print smallest_contiguous(s, 5)
print smallest_contiguous(s, 6)
音盲 2024-10-13 14:42:23

这是我想出的一个:

def smallest_contiguous(s,N):
    try:
        result = []
        while len(result) < N:
            min_value = min(s)
            s.remove(min_value)
            if result == [] or min_value == result[-1] + 1:
                result.append(min_value)
            else:
                result = [min_value]
        return result
    except ValueError:
        return []

它会修改输入集作为副作用。

Here's one I came up with:

def smallest_contiguous(s,N):
    try:
        result = []
        while len(result) < N:
            min_value = min(s)
            s.remove(min_value)
            if result == [] or min_value == result[-1] + 1:
                result.append(min_value)
            else:
                result = [min_value]
        return result
    except ValueError:
        return []

It modifies the input set as a side effect.

无尽的现实 2024-10-13 14:42:23

itertools 来救援。 groupby 在这里完成所有繁重的工作
由于调用了 sorted(),该算法的复杂度为 O(n logn)

>>> from itertools import groupby, count
>>> def smallest_contiguous(s, N):
...     for i,j in groupby(sorted(s), key=lambda i,c=count().next: i-c()):
...         res = list(j)
...         if len(res) == N:
...             return res
...     return []

... 
>>> smallest_contiguous(s,5)
[40, 41, 42, 43, 44]
>>> smallest_contiguous(s,6)
[]

itertools to the rescue. groupby does all the grunt work here
The algorithm is O(n logn) because of the call to sorted()

>>> from itertools import groupby, count
>>> def smallest_contiguous(s, N):
...     for i,j in groupby(sorted(s), key=lambda i,c=count().next: i-c()):
...         res = list(j)
...         if len(res) == N:
...             return res
...     return []

... 
>>> smallest_contiguous(s,5)
[40, 41, 42, 43, 44]
>>> smallest_contiguous(s,6)
[]
一杯敬自由 2024-10-13 14:42:23
def smallest_contiguous(s, n):
    xs = sorted(s)
    return next(x for i, x in enumerate(xs) if xs[i + n - 1] == x + n - 1)
def smallest_contiguous(s, n):
    xs = sorted(s)
    return next(x for i, x in enumerate(xs) if xs[i + n - 1] == x + n - 1)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文