Python 中最长的不同连续列表
我有一个列表:
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
假设您的列表已排序:
预计到达时间:修复最后一步;
Assuming your list is sorted:
ETA: fixed last step;
最简单的事情似乎是循环遍历列表一次,构建您可以找到的任何序列,然后打印最长的序列。
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.
可能有一种更Pythonic的方法,但我现在想不到,所以这里有一个非常基本的解决方案:
There is probably a more pythonic way, but I can't think of it right now, so here a very basic solution: