代码之家  ›  专栏  ›  技术社区  ›  user225312

在python中查找字符串中多次出现的字符串

  •  59
  • user225312  · 技术社区  · 15 年前

    如何在Python中查找字符串中多次出现的字符串?考虑一下:

    >>> text = "Allowed Hello Hollow"
    >>> text.find("ll")
    1
    >>> 
    

    所以第一次出现 ll 如预期的那样位于1。如何找到下一次发生的事件?

    同样的问题对列表有效。考虑:

    >>> x = ['ll', 'ok', 'll']
    

    我怎么找到所有的 陆上通信线 有他们的索引吗?

    17 回复  |  直到 7 年前
        1
  •  95
  •   poke    11 年前

    re.finditer

    >>> import re
    >>> text = 'Allowed Hello Hollow'
    >>> for m in re.finditer('ll', text):
             print('ll found', m.start(), m.end())
    
    ll found 1 3
    ll found 10 12
    ll found 16 18
    

    str.find

    >>> text = 'Allowed Hello Hollow'
    >>> index = 0
    >>> while index < len(text):
            index = text.find('ll', index)
            if index == -1:
                break
            print('ll found at', index)
            index += 2 # +2 because len('ll') == 2
    
    ll found at  1
    ll found at  10
    ll found at  16
    

        2
  •  23
  •   FReeze FRancis    9 年前

    string.count

    "Allowed Hello Hollow".count('ll')
    >>> 3
    


        3
  •  19
  •   bstpierre Edgar Aviles    15 年前

    >>> l = ['ll', 'xx', 'll']
    >>> print [n for (n, e) in enumerate(l) if e == 'll']
    [0, 2]
    

    >>> text = "Allowed Hello Hollow"
    >>> print [n for n in xrange(len(text)) if text.find('ll', n) == n]
    [1, 10, 16]
    

    >>> text = 'Alllowed Hello Holllow'
    >>> print [n for n in xrange(len(text)) if text.find('ll', n) == n]
    [1, 2, 11, 17, 18]
    
        4
  •  12
  •   Community Mohan Dere    8 年前

    poke's solution

    str.index ValueError

    def findall(sub, string):
        """
        >>> text = "Allowed Hello Hollow"
        >>> tuple(findall('ll', text))
        (1, 10, 16)
        """
        index = 0 - len(sub)
        try:
            while True:
                index = string.index(sub, index + len(sub))
                yield index
        except ValueError:
            pass
    

    str.find -1 iter

    def findall_iter(sub, string):
        """
        >>> text = "Allowed Hello Hollow"
        >>> tuple(findall_iter('ll', text))
        (1, 10, 16)
        """
        def next_index(length):
            index = 0 - length
            while True:
                index = string.find(sub, index + length)
                yield index
        return iter(next_index(len(sub)).next, -1)
    

    def findall_each(findall, sub, strings):
        """
        >>> texts = ("fail", "dolly the llama", "Hello", "Hollow", "not ok")
        >>> list(findall_each(findall, 'll', texts))
        [(), (2, 10), (2,), (2,), ()]
        >>> texts = ("parallellized", "illegally", "dillydallying", "hillbillies")
        >>> list(findall_each(findall_iter, 'll', texts))
        [(4, 7), (1, 6), (2, 7), (2, 6)]
        """
        return (tuple(findall(sub, string)) for string in strings)
    
        5
  •  3
  •   chauncey    15 年前

    In [1]: x = ['ll','ok','ll']
    
    In [2]: for idx, value in enumerate(x):
       ...:     if value == 'll':
       ...:         print idx, value       
    0 ll
    2 ll
    

    In [3]: x = ['Allowed','Hello','World','Hollow']
    
    In [4]: for idx, value in enumerate(x):
       ...:     if 'll' in value:
       ...:         print idx, value
       ...:         
       ...:         
    0 Allowed
    1 Hello
    3 Hollow
    
        6
  •  2
  •   ghostdog74    15 年前
    >>> for n,c in enumerate(text):
    ...   try:
    ...     if c+text[n+1] == "ll": print n
    ...   except: pass
    ...
    1
    10
    16
    
        7
  •  1
  •   Aaron Semeniuk    13 年前

    needle = input()
    haystack = input()
    counter = 0
    n=-1
    for i in range (n+1,len(haystack)+1):
       for j in range(n+1,len(haystack)+1):
          n=-1
          if needle != haystack[i:j]:
             n = n+1
             continue
          if needle == haystack[i:j]:
             counter = counter + 1
    print (counter)
    
        8
  •  0
  •   beardc    9 年前

    def find_all(st, substr, start_pos=0, accum=[]):
        ix = st.find(substr, start_pos)
        if ix == -1:
            return accum
        return find_all(st, substr, start_pos=ix + 1, accum=accum + [ix])
    

    findall_lc = lambda txt, substr: [n for n in xrange(len(txt))
                                       if txt.find(substr, n) == n]
    

    import random, string; random.seed(0)
    s = ''.join([random.choice(string.ascii_lowercase) for _ in range(100000)])
    
    >>> find_all(s, 'th') == findall_lc(s, 'th')
    True
    >>> findall_lc(s, 'th')[:4]
    [564, 818, 1872, 2470]
    

    %timeit find_all(s, 'th')
    1000 loops, best of 3: 282 µs per loop
    
    %timeit findall_lc(s, 'th')    
    10 loops, best of 3: 92.3 ms per loop
    
        9
  •  0
  •   pmsh.93    9 年前
    #!/usr/local/bin python3
    #-*- coding: utf-8 -*-
    
    main_string = input()
    sub_string = input()
    
    count = counter = 0
    
    for i in range(len(main_string)):
        if main_string[i] == sub_string[0]:
            k = i + 1
            for j in range(1, len(sub_string)):
                if k != len(main_string) and main_string[k] == sub_string[j]:
                    count += 1
                    k += 1
            if count == (len(sub_string) - 1):
                counter += 1
            count = 0
    
    print(counter) 
    

        10
  •  0
  •   Elias Zamaria    9 年前

    str.index

    def all_substring_indexes(string, substring, start=0, end=None):
        result = []
        new_start = start
        while True:
            try:
                index = string.index(substring, new_start, end)
            except ValueError:
                return result
            else:
                result.append(index)
                new_start = index + len(substring)
    
        11
  •  0
  •   FReeze FRancis    9 年前

            def allindices(string, sub):
               l=[]
               i = string.find(sub)
               while i >= 0:
                  l.append(i)
                  i = string.find(sub, i + 1)
               return l
    
        12
  •  0
  •   Kenly    9 年前

    >>> key = 'll'
    >>> text = "Allowed Hello Hollow"
    >>> x = [len(i) for i in text.split(key)[:-1]]
    >>> [sum(x[:i+1]) + i*len(key) for i in range(len(x))]
    [1, 10, 16]
    >>> 
    
        13
  •  0
  •   blasrodri    8 年前

    def retrieve_occurences(sequence, word, result, base_counter):
         indx = sequence.find(word)
         if indx == -1:
             return result
         result.append(indx + base_counter)
         base_counter += indx + len(word)
         return retrieve_occurences(sequence[indx + len(word):], word, result, base_counter)
    
        14
  •  0
  •   Abhishek J    8 年前
        15
  •  0
  •   rdo    8 年前

        >>> text = 'Allowed Hello Hollow'
        >>> place = 0
        >>> while text.find('ll', place) != -1:
                print('ll found at', text.find('ll', place))
                place = text.find('ll', place) + 2
    
    
        ll found at 1
        ll found at 10
        ll found at 16
    
        16
  •  0
  •   Stefan Gruenwald    8 年前

    string1= "Allowed Hello Hollow"
    string2= "ll"
    print [num for num in xrange(len(string1)-len(string2)+1) if string1[num:num+len(string2)]==string2]
    # [1, 10, 16]
    
        17
  •  0
  •   Mystearica Primal Fende    7 年前

    findin = "algorithm alma mater alison alternation alpines"
    search = "al"
    inx = 0
    num_str = 0
    
    while True:
        inx = findin.find(search)
        if inx == -1: #breaks before adding 1 to number of string
            break
        inx = inx + 1
        findin = findin[inx:] #to splice the 'unsearched' part of the string
        num_str = num_str + 1 #counts no. of string
    
    if num_str != 0:
        print("There are ",num_str," ",search," in your string.")
    else:
        print("There are no ",search," in your string.")