Python 中最长的不同连续列表

发布于 2024-09-26 04:06:14 字数 231 浏览 5 评论 0原文

我有一个列表:

a = [2, 3, 5, 6, 6, 7, 10, 11, 13, 14, 15, 16, 16, 17, 18, 20, 21]

是否可以创建一个函数来显示最长的不同连续元素列表?

请展示如何做到这

一点在这种情况下,答案应该是:

13, 14, 15, 16, 17, 18

I have a list:

a = [2, 3, 5, 6, 6, 7, 10, 11, 13, 14, 15, 16, 16, 17, 18, 20, 21]

Is it possible to make a function that shows the longest list of distinct, consecutive elements?

Please, show how to do it

In this case the answer should be:

13, 14, 15, 16, 17, 18

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

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

发布评论

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

评论(3

不弃不离 2024-10-03 04:06:14

假设您的列表已排序:

>>> from itertools import groupby
>>> z = zip(a, a[1:])
>>> tmp = [list(j) for i, j in groupby(z, key=lambda x: (x[1] - x[0]) <= 1)]
>>> max(tmp, key=len)
[(13, 14), (14, 15), (15, 16), (16, 16), (16, 17), (17, 18)]
>>> list(range(_[0][0], _[-1][-1]+1))
[13, 14, 15, 16, 17, 18]

预计到达时间:修复最后一步;

Assuming your list is sorted:

>>> from itertools import groupby
>>> z = zip(a, a[1:])
>>> tmp = [list(j) for i, j in groupby(z, key=lambda x: (x[1] - x[0]) <= 1)]
>>> max(tmp, key=len)
[(13, 14), (14, 15), (15, 16), (16, 16), (16, 17), (17, 18)]
>>> list(range(_[0][0], _[-1][-1]+1))
[13, 14, 15, 16, 17, 18]

ETA: fixed last step;

极度宠爱 2024-10-03 04:06:14

最简单的事情似乎是循环遍历列表一次,构建您可以找到的任何序列,然后打印最长的序列。

a = [2, 3, 5, 6, 6, 7, 10, 11, 13, 14, 15, 16, 16, 17, 18, 20, 21]

seqlist = [] # List of Sequences
seq = []     # Current Sequence
last = -1

for item in a:
   # Start a new sequence if the gap from the last item is too big
   if item - last > 1:
       seqlist.append(seq)
       seq = []

   # only add item to the sequence if it's not the same as the last
   if item != last:
        seq.append(item)

   last = item

# Print longest sequence found
print max(seqlist)

The simplest thing to do seems to be to loop through the list once, building any sequences you can find and then print the longest one.

a = [2, 3, 5, 6, 6, 7, 10, 11, 13, 14, 15, 16, 16, 17, 18, 20, 21]

seqlist = [] # List of Sequences
seq = []     # Current Sequence
last = -1

for item in a:
   # Start a new sequence if the gap from the last item is too big
   if item - last > 1:
       seqlist.append(seq)
       seq = []

   # only add item to the sequence if it's not the same as the last
   if item != last:
        seq.append(item)

   last = item

# Print longest sequence found
print max(seqlist)
往事随风而去 2024-10-03 04:06:14

可能有一种更Pythonic的方法,但我现在想不到,所以这里有一个非常基本的解决方案:

def longestDistinctConsecutiveList ( lst ):
    lst = list( set( lst ) ) # get rid of duplicated elements
    lst.sort() # sort

    s, l = 0, 0
    for i in range( len( lst ) ):
        for j in range( i, len( lst ) ):
            if lst[j] - lst[i] == len( lst[i:j] ) > l:
                l = 1 + a[j] - a[i]
                s = i
    return lst[s:s+l]

There is probably a more pythonic way, but I can't think of it right now, so here a very basic solution:

def longestDistinctConsecutiveList ( lst ):
    lst = list( set( lst ) ) # get rid of duplicated elements
    lst.sort() # sort

    s, l = 0, 0
    for i in range( len( lst ) ):
        for j in range( i, len( lst ) ):
            if lst[j] - lst[i] == len( lst[i:j] ) > l:
                l = 1 + a[j] - a[i]
                s = i
    return lst[s:s+l]
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文