Python 最大数组,按与给定数组相反的顺序排序

Python 最大数组,按与给定数组相反的顺序排序,python,list,sorting,Python,List,Sorting,最大的数组是什么,按与给定数组相反的顺序排序 Input: [25,28,11,10,9, 8,17, 23,20,19,14] Output:[[28,11,10,9,8],[23,20,19,14]] 我尝试了谷歌,但没有找到任何解决方案,请帮助我解决这个问题。下面是我试图编写的代码,但不太清楚 def findRuns(L): result = [] start = 0 n = L[start] for i,num in enumerate(L):

最大的数组是什么,按与给定数组相反的顺序排序

Input: [25,28,11,10,9, 8,17, 23,20,19,14] 

Output:[[28,11,10,9,8],[23,20,19,14]]
我尝试了谷歌,但没有找到任何解决方案,请帮助我解决这个问题。下面是我试图编写的代码,但不太清楚

 def findRuns(L):
    result = []
    start = 0
    n = L[start]
    for i,num in enumerate(L):
        if num <= n:
            n = num
        else:
            answer.append(L[start:1])
            start = i
            n = L[i]
    result.append(L[start])
    return result

请帮我解决这个问题。提前感谢。

您可以编写一个grouper函数,然后使用max:

def findRuns(L):
    answer = []
    start = 0
    n = L[start]
    for i,num in enumerate(L[1:],1):
        if num <= n:
            n = num
        else:
            answer.append(L[start:i])
            start = i
            n = L[start]
    answer.append(L[start:])
    return answer

那么您是想用Python、Ruby还是C来实现这一点?第二个输入的输出似乎不正确。这是您代码的输出,还是预期的输出?任何编程,但更喜欢python bcs我正在使用python解决这个问题。
In [48]: findRuns([25,28,11,10,9, 8, 23] )
Out[48]: [[25], [28, 11, 10, 9, 8], [23]]

In [49]: findRuns([25,28,11,10,9, 8,17, 23,20,19,14] )
Out[49]: [[25], [28, 11, 10, 9, 8], [17], [23, 20, 19, 14]]
def grouper(seq):
    it = iter(seq)
    grp = [next(it)]
    for item in it:
        if item <= grp[-1]:
            grp.append(item)
        else:
            yield grp
            grp = [item]
    if grp:
        yield grp

inp1 = [25,28,11,10,9, 8, 23] 
inp2 = [25,28,11,10,9, 8,17, 23,20,19,14] 

print max(grouper(inp1), key=len)
#[28, 11, 10, 9, 8]
print max(grouper(inp2), key=len)
#[28, 11, 10, 9, 8]
print list(grouper(inp2))
#[[25], [28, 11, 10, 9, 8], [17], [23, 20, 19, 14]]