【问题标题】:Find longest non-breaking common elements in a list在列表中查找最长的不间断公共元素
【发布时间】:2014-12-15 03:03:40
【问题描述】:

我有这个列表,其中只包含 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']

[6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16]

我怎样才能做到这一点?

【问题讨论】:

  • @thefourtheye:最后一个列表是最长Ss的lsindex
  • 更正了答案。

标签: python algorithm list


【解决方案1】:

itertools.groupbyenumeratemax 一起使用:

>>> from operator import itemgetter
>>> from itertools import groupby
>>> val = max((list(g) for k, g in
                   groupby(enumerate(ls), itemgetter(1)) if k == 'S'), key=len)
>>> indices, items = zip(*val)
>>> indices
(6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16)
>>> items
('S', 'S', 'S', 'S', 'S', 'S', 'S', 'S', 'S', 'S', 'S')

【讨论】:

  • 你需要添加一些东西来特别选择S,但是是的,我也在写一个groupby sol'n。 :-)
  • @DSM 谢谢!完全没有达到这个要求。
  • 有了你优雅的条件,我该如何修改以包含列表中没有“S”出现的情况?
  • @pdubois 添加k != "S"?
  • @pdubois 我明白你的意思。在 Python 3.4+ 中,可以为 max 指定默认值,以防传递给它的可迭代对象为空。在其他版本中,您可以简单地添加 if 条件,例如:indices = [],items=[];if 'S' in ls: groupby...
【解决方案2】:

与 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))))

【讨论】:

    【解决方案3】:
    >>> 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]
    

    【讨论】:

      【解决方案4】:

      包 com.algo.sort;

      公共类 LargestCont{

      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);
      
      }
      

      }

      【讨论】:

        【解决方案5】:
        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"))
        

        我找到了这个。

        【讨论】:

        • 这似乎并没有产生 OP 所追求的东西。
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-01-25
        • 1970-01-01
        • 1970-01-01
        • 2018-03-15
        • 2021-02-14
        相关资源
        最近更新 更多