Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/list/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何在列表中按字母顺序取出最长的子字符串?_Python_List_Sorting_Substring - Fatal编程技术网

Python 如何在列表中按字母顺序取出最长的子字符串?

Python 如何在列表中按字母顺序取出最长的子字符串?,python,list,sorting,substring,Python,List,Sorting,Substring,例如,初始列表L=[“by”,“article”,“circle”,“For”,“line”,“dance”,“efforce”],我想去掉[“article”,“circle”,“For”,“line”]作为结果。因为L中的第一个字母是(b,a,c,f,L,d,e),并且(a,c,f,L)的结果比(d,e)长,所以结果是[“文章”,“圆圈”,“for”,“line”]。如何修复我的代码?我是新来的,所以我对sorted()和嵌套循环非常困惑 def find_longest_consistin

例如,初始列表L=[“by”,“article”,“circle”,“For”,“line”,“dance”,“efforce”],我想去掉[“article”,“circle”,“For”,“line”]作为结果。因为L中的第一个字母是(b,a,c,f,L,d,e),并且(a,c,f,L)的结果比(d,e)长,所以结果是[“文章”,“圆圈”,“for”,“line”]。如何修复我的代码?我是新来的,所以我对sorted()和嵌套循环非常困惑

def find_longest_consisting(test_case):
    if not test_case:
        return []
    result = []
    for i in range(len(test_case)):
        for j in range(i + len(result) + 1):
            substring = test_case[i:j]
            if len(substring) != (j - i):
                break
            if sorted(substring) == list(substring):
                result = substring
    return result

test_cases = ["by","article","circle","for","line","dance","effort"]
test_result = find_longest_consisting(test_cases)
print(test_result)

实现这一点的最快方法是维护一个“最长”列表,并在遇到排序更改时将其长度与当前子列表进行比较,如果当前子列表较长,则将其分配给最长的子列表。这只需要对列表进行一次迭代即可获得结果

def longest_group(lst):
    longest = []
    current = lst[:1]
    for i in range(1, len(lst)):
        if lst[i][0] <= lst[i-1][0]:
            if len(current) > len(longest):
                longest = current
            current = [lst[i]]
        else:
            current.append(lst[i])
    if len(current) > len(longest):
        longest = current
    return longest

print(longest_group(["by","article","circle","for","line","dance","effort"]))
print(longest_group(["zoo","slack","egg"]))

对于您的
[“by”,“article”,“circle”,“For”,“line”,“dance”,“efforce”]
列表,这比从列表中排序所有组合快10倍。请参阅以获取演示。

除了使用自己的嵌套循环来检查不同的子集之外,一个简单的替代方法是使用
itertools。组合

from itertools import combinations
def longest_group(lst):
    return max((
        lst[a:b]
        for a, b in combinations(range(len(lst)+1), 2)
        if sorted(lst[a:b]) == lst[a:b]
    ), key=len) if lst else lst
如果速度比简单更重要,那么最快的版本将是迭代列表并跟踪对应于最长排序切片的范围:

def longest_group(lst):
    a, b, i = 0, 0, 0
    for j, e in enumerate(lst[1:]):
        if e < lst[j]:
            if j - i > b - a:
                a, b = i, j
            i = j + 1
    if len(lst) - i > b - a + 1:
        a, b = i, len(lst)
    return lst[a:b+1]
def最长_组(lst):
a、 b,i=0,0,0
对于枚举中的j,e(lst[1:]):
如果eb-a:
a、 b=i,j
i=j+1
如果len(lst)-i>b-a+1:
a、 b=i,len(lst)
返回lst[a:b+1]

这比简单的列表迭代要慢10倍左右。添加了一个更优化的版本。;)Cool-OP得到了一个更好的解决方案。唯一的缺点是
最长的组([“zoo”、“slack”、“egg”])
的结果是
[“egg”]
,而不是
[“zoo”]
(请参见编辑历史记录)。分配新列表比简单地切片输入慢约2倍。这些答案中有一个解决了你的问题吗?如果没有,你能提供更多的信息来帮助回答这个问题吗?否则,请考虑将最能解决你的问题的答案标记为(在上/下投票箭头下的复选标记)。看到和
def longest_group(lst):
    a, b, i = 0, 0, 0
    for j, e in enumerate(lst[1:]):
        if e < lst[j]:
            if j - i > b - a:
                a, b = i, j
            i = j + 1
    if len(lst) - i > b - a + 1:
        a, b = i, len(lst)
    return lst[a:b+1]