在列表中找到连续序列的第一个数字

huangapple go评论70阅读模式
英文:

How to find the first number of multiple consecutive sequence in list

问题

我使用输入
[0,2,3,4,6,7,8,16,17,18,21,23,24,26,34,35,36,37,38,40,41,46,47]

但输出
[0,2,6,16,21,23,26,34,40,46] 是错误的
应该是 [2,6,16,23,34,40,46]

如何找到列表中多个连续序列的第一个数字?

如果可能的话,是否可以修改以找到最后一个数字
例如 [4, 8, 18, 24, 38, 41, 47]

def find_consecutive_numbers(lst):
    result = []
    i = 0
    while i < len(lst)-2:
        if lst[i]+1 == lst[i+1] and lst[i+1]+1 == lst[i+2]:
            result.append(lst[i])
            i += 2
        else:
            i += 1
    return result

我曾尝试询问ChatGPT,但结果并不完美。
它首先使用字符串处理。
但列表中也有一些意外的数字。

英文:

I use input
[0,2,3,4,6,7,8,16,17,18,21,23,24,26,34,35,36,37,38,40,41,46,47]

But output
[0,2,6,16,21,23,26,34,40,46] which is wrong
Should be [2,6,16,23,34,40,46]

How to find the first number of multiple consecutive sequence in list?

If it is possible, can be editable to find the last one too
Such that [4, 8, 18, 24, 38, 41, 47]

def find_consecutive_numbers(lst):
    result = []
    i = 0
    while i &lt; len(lst)-2:
        if lst[i]+1 == lst[i+1] and lst[i+1]+1 == lst[i+2]:
            result.append(lst[i])
            i += 2
        else:
            i += 1
    return result

I had tried to ask chatgpt, but result is not perfect.
It use string to do first.
But some unexpected numbers in list too

答案1

得分: 0

尝试按照与前一个元素的差异进行分组,并返回每个差异为1的组中的第一个元素:

from itertools import pairwise, groupby

def find_consecutive_numbers(lst, last=False):
    for k, g in groupby(pairwise(lst), key=lambda x: x[1]-x[0]):
        if k == 1:
            if last:
                *_, (_, end) = g
                yield end
            else:
                yield next(g)[0]

# 示例用法
>>> list(find_consecutive_numbers(l))
[2, 6, 16, 23, 34, 40, 46]
>>> list(find_consecutive_numbers(l, last=True))
[4, 8, 18, 24, 38, 41, 47]

一些文档链接
* [`itertools.pairwise`][0]
* [`itertools.groupby`][1]
* [生成器函数 (`yield`)][2]
* [`next`][3]

[0]: https://docs.python.org/3/library/itertools.html#itertools.pairwise
[1]: https://docs.python.org/3/library/itertools.html#itertools.groupby
[2]: https://stackoverflow.com/questions/231767/what-does-the-yield-keyword-do-in-python
[3]: https://docs.python.org/3/library/functions.html#next
英文:

Try grouping by difference to preceding element and yield the first of each group that difference is 1:

from itertools import pairwise, groupby

def find_consecutive_numbers(lst, last=False):
    for k, g in groupby(pairwise(lst), key=lambda x: x[1]-x[0]):
        if k == 1:
            if last:
                *_, (_, end) = g
                yield end
            else:
                yield next(g)[0]

&gt;&gt;&gt; list(find_consecutive_numbers(l))
[2, 6, 16, 23, 34, 40, 46]
&gt;&gt;&gt; list(find_consecutive_numbers(l, last=True))
[4, 8, 18, 24, 38, 41, 47]

Some docs:

huangapple
  • 本文由 发表于 2023年6月29日 18:50:15
  • 转载请务必保留本文链接:https://go.coder-hub.com/76580335.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定