String count with overlapping occurrences

前端 未结 22 3101
耶瑟儿~
耶瑟儿~ 2020-11-21 23:25

What\'s the best way to count the number of occurrences of a given string, including overlap in Python? This is one way:

def function(string, str_to_search_f         


        
22条回答
  •  面向向阳花
    2020-11-21 23:36

    Given

    sequence = '1011101111'
    sub = "11"
    

    Code

    In this particular case:

    sum(x == tuple(sub) for x in zip(sequence, sequence[1:]))
    # 5
    

    More generally, this

    windows = zip(*([sequence[i:] for i, _ in enumerate(sequence)][:len(sub)]))
    sum(x == tuple(sub) for x in windows)
    # 5
    

    or extend to generators:

    import itertools as it
    
    
    iter_ = (sequence[i:] for i, _ in enumerate(sequence))
    windows = zip(*(it.islice(iter_, None, len(sub))))
    sum(x == tuple(sub) for x in windows)
    

    Alternative

    You can use more_itertools.locate:

    import more_itertools as mit
    
    
    len(list(mit.locate(sequence, pred=lambda *args: args == tuple(sub), window_size=len(sub))))
    # 5
    

提交回复
热议问题