$ python
Python 3.7.7 (default, May 7 2020, 21:25:33)
[GCC 7.3.0] :: Anaconda, Inc. on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> from itertools import groupby as g
>>> d, e, l, λ, m = [1,2,3,2,4,5,6,7,8,1,0,4,5,6], enumerate, len, list, max
>>> [i[1]for i in m((λ(s)for k,s in g(e(d),lambda j:j[0]-j[1])),key=lambda j:l(j))]
[4, 5, 6, 7, 8]
>>>
from itertools import groupby
from operator import itemgetter
new_l = []
for k, g in groupby(enumerate(data), lambda ix : ix[0] - ix[1]):
new_l.append(list(map(itemgetter(1), g)))
print(max(new_l, key=lambda x: len(x)))
a = [1,2,3,2,4,5,6,7,8,1,0,4,5,6]
large_seq = []
large_seq_len = 0
temp_seq = []
temp_seq_len = 0
x = None
for i in a:
if x is None or x + 1 == i:
temp_seq.append(i)
temp_seq_len += 1
else:
if temp_seq_len > large_seq_len:
large_seq = temp_seq
large_seq_len = temp_seq_len
temp_seq = [i]
temp_seq_len = 1
x = i
if temp_seq_len > large_seq_len:
large_seq = temp_seq
print(large_seq)
Sociopath's answer可以折叠成一个相对较短的内衬
你能行
输出:
一种不使用内置python模块且代码复杂度为O(n)的方法
https://www.online-python.com/XNjaLm9vMS
相关问题 更多 >
编程相关推荐