Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/spring-boot/5.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_Algorithm_List - Fatal编程技术网

Python 查找列表中最长的非中断公共元素

Python 查找列表中最长的非中断公共元素,python,algorithm,list,Python,Algorithm,List,我的列表只包含Ws和Ss: ls = ['W', 'S', 'S', 'S', 'W', 'W', 'S', 'S', 'S', 'S', 'S', 'S', 'S', 'S', 'S', 'S', 'S', 'W', 'W', 'W', 'W', 'W', 'W', 'S'] 我想做的是提取列表中最长的不间断“S”? 并返回该Ss的索引,返回: ['S', 'S', 'S', 'S', 'S', 'S', 'S', 'S', 'S', 'S', 'S'] 及 如何实现这一点?与和一起使用

我的列表只包含Ws和Ss:

ls = ['W', 'S', 'S', 'S', 'W', 'W', 'S', 'S', 'S', 'S', 'S', 'S', 'S', 'S', 'S', 'S', 'S', 'W', 'W', 'W', 'W', 'W', 'W', 'S'] 
我想做的是提取列表中最长的不间断“S”? 并返回该Ss的索引,返回:

['S', 'S', 'S', 'S', 'S', 'S', 'S', 'S', 'S', 'S', 'S']

如何实现这一点?

与和一起使用:


我找到了这个。

与Ashwini Chaudhary的相同解决方案减去优雅

from itertools import groupby

index, result, m_index = 0, [], 0

# Group based on the elements of the list
for item, grp in groupby(ls):
    # Get the grouped items as a list
    grp = list(grp)
    # Filter out `M`s and check if this is the biggest run of `S`s ever seen
    if item == "S" and len(grp) > len(result):
        result, m_index = grp, index
    # Increment the index to keep track of the list covered
    index += len(grp)

print(result, list(range(m_index, m_index + len(result))))
包com.algo.sort

公共类最大控制{

static int g[]={1,1,1,1,2,2,3,4,5,5,5,5,5,5,5,5,5,5,5,2,2,2,2,3,3,3,3,3,3,7,7,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,3,3,3,3,3,3,3,3,3,0,0,0,0,0,0,0,0,0};


public static void main(String f[]){
    manage(g);
}


public static void manage(int[] j){
    int max=1; int val=-1; int count=0; int ans=0;
    for(int i=0;i<j.length;i++){
        if(j[i]!=val){
            if(max>count){
                ans=val;
                count=max;
                System.out.println(ans+"...."+count);                   
            }
        val=j[i]; max=1;}else{
            max++;
        }                   
    }

    System.out.println(ans);

}
static int g[]={1,1,1,1,2,2,3,4,5,5,5,5,5,5,5,2,2,2,3,3,3,3,3,3,3,7,8,8,8,8,8,8,8,8,8,3,3,0,0,0};
公共静态void main(字符串f[]{
管理(g);
}
公共静态无效管理(int[]j){
int max=1;int val=-1;int count=0;int ans=0;
for(int i=0;i计数){
ans=val;
计数=最大值;
System.out.println(ans+“…”+计数);
}
val=j[i];max=1;}else{
max++;
}                   
}
系统输出打印LN(ans);
}

}

@thefourtheye:最后一个列表是最长Ss的
ls
索引。更正了答案。你需要添加一些东西来特别挑选
S
,但是是的,我也在写一个
群组比
sol'n.:-)@DSM谢谢!完全没有达到这个要求。有了您优雅的条件,我如何修改以包括列表中没有出现“S”的情况?@pdubois Add
k!=“S”
?@pdubois我明白你的意思。在Python 3.4+中,如果传递给它的iterable为空,则可以将默认值指定为
max
。在其他版本中,您可以简单地添加一个
if
条件,例如:
索引=[],项=[];如果ls:groupby中的'S…
这似乎不会产生OP想要的东西。
ls = ['W', 'S', 'S', 'S', 'W', 'W', 'S', 'S', 'S', 'S', 'S', 'S', 'S', 'S', 'S', 'S', 'S', 'W', 'W', 'W', 'W', 'W', 'W', 'S']
for x,y in enumerate(ls):
    print (x,str(y).split("W"))
from itertools import groupby

index, result, m_index = 0, [], 0

# Group based on the elements of the list
for item, grp in groupby(ls):
    # Get the grouped items as a list
    grp = list(grp)
    # Filter out `M`s and check if this is the biggest run of `S`s ever seen
    if item == "S" and len(grp) > len(result):
        result, m_index = grp, index
    # Increment the index to keep track of the list covered
    index += len(grp)

print(result, list(range(m_index, m_index + len(result))))
>>> import re
>>> max((x.group(), x.span()) for x in re.finditer("S+", "".join(ls)))
('SSSSSSSSSSS', (6, 17))

>>> range(6, 17)
[6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16]
static int g[]={1,1,1,1,2,2,3,4,5,5,5,5,5,5,5,5,5,5,5,2,2,2,2,3,3,3,3,3,3,7,7,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,3,3,3,3,3,3,3,3,3,0,0,0,0,0,0,0,0,0};


public static void main(String f[]){
    manage(g);
}


public static void manage(int[] j){
    int max=1; int val=-1; int count=0; int ans=0;
    for(int i=0;i<j.length;i++){
        if(j[i]!=val){
            if(max>count){
                ans=val;
                count=max;
                System.out.println(ans+"...."+count);                   
            }
        val=j[i]; max=1;}else{
            max++;
        }                   
    }

    System.out.println(ans);

}