How to make a function return a list of indices of the characters in the second string that appears in the first string?

后端 未结 4 1816
孤街浪徒
孤街浪徒 2021-01-25 08:54
def get_indices_from_the_second_string(string1, string2):
    \'\'\'(str, str) -> list of int
    >>> get_indices_from_the_second_string(\'AGTACACGTTAC\', \'         


        
4条回答
  •  余生分开走
    2021-01-25 09:31

    def get_indices_from_the_second_string(string1, string2):
        acc = []
        s2_counter = 0
        for i, letter in enumerate(string1):
            if letter == string2[s2_counter]:
                acc.append(i)
                s2_counter += 1
                if len(acc) == len(string2):
                    break
        return acc
    
    a = get_indices_from_the_second_string('GAATTCCGTTAC', 'GAATTC')
    

提交回复
热议问题