Python:如何查找连续系列中第一个和最后一个字符的索引

时间:2017-08-01 14:18:22

标签: python python-3.x

假设我有string =“aaabaa”

使用这些信息,我想找到第一个和最后一个'a'的索引

编辑:抱歉模棱两可:最长的连续字符串'a'是aaa,所以我想找到索引0和2,而不是0和5.

我可以使用Ignacio Vazquez-Abrams的回复代码找到最长的a的长度:Counting longest occurrence of repeated sequence in Python

4 个答案:

答案 0 :(得分:2)

第一个索引:

myString.find('a')

最后一个索引:

myString.rfind('a')

连续a的最长系列的长度:

counter = 0
maxCount = 0
for c in myString:
    if c == 'a':
        counter+=1
        if counter > maxCount:
            maxCount = counter
    else:
        counter = 0

答案 1 :(得分:1)

我认为在字符串s中找到最长系列的连续c是一种简单的方法:

def find_consecutive(s, c):
    len = 0
    len_list = []
    for x in s:
        if x == c:
            if len==0:
                len = 1
            else:
                len  += 1
        else:
            len_list.append(len)
            len = 0
    return max(len_list)

答案 2 :(得分:1)

这对你来说是个不错的功能。而且我认为您想知道起始索引和结束索引在哪里出现最长时间。

def find_longest_a(string):

    start_index = 0
    current_index = 0
    end_index = 0
    count_a = 0

    max_a = 0
    max_a_start_index = 0
    max_a_end_index = 0

    for character in string:
        if character == 'a':
            count_a += 1
            end_index = current_index

        elif character != 'a':
            if count_a > max_a:
                max_a = count_a
                max_a_start_index = start_index
                max_a_end_index = end_index

            start_index = current_index + 1
            count_a = 0

        current_index += 1

    if count_a > max_a:
        max_a = count_a
        max_a_start_index = start_index
        max_a_end_index = end_index

    return max_a, max_a_start_index, max_a_end_index

如果你运行这个功能:

>>> find_longest_a("aabs?1jlkdaklaa aa a aaaasnd a")

它会返回:

(4, 21, 24)

4是a的数量,21是该序列开始的索引,24是该序列结束的位置。

答案 3 :(得分:1)

所以这就是我想出来的。没有做任何花哨的事情,所以步骤可以很清楚。我首先寻找我们正在寻找的角色的第一次出现。找到后,i是起始索引。然后我继续循环通过字符串,直到我找到一个不是我们想要的字符。当我找到一个时,它意味着结束索引是current index - 1。我将这些值附加到开始列表和结束列表。然后我重置值并继续前进。这样我就可以获得多组连续的重复字符串。

最后我使用zip()来创建起始和结束索引的元组。

def find_indices(str, toFind):
    found = False #tells us if what we are searching for is found
    start = [] #holds start inds
    end = [] #hold end inds
    i = 0   #tracks index
    for letter in str:
        if letter == toFind: #found the letter
            if found == False:
                found = True
                start.append(i) #first index
        if letter != toFind and found == True: #found the character after the last character we wanted
            end.append(i - 1) #we are at the character after the we want so index - 1
            found = False #not found anymore so so found is false
        i += 1

    #handles the case where the last letter is one we care about since the for loop is looking for char
    #after the one we want and in this case the one we want is last. 
    if str[-1:] == toFind:
        end.append(len(str)-1)

    #combine the starts and end into tuples of starts and ends. 
    return zip(start, end)

如果你运行这个功能,你得到:

(Pdb) inds = find_indices('aaabaa', 'a')

`(Pdb)inds'

[(0, 2), (4, 5)]

使用更大的字符串:

x = 'aaabaakjhasdfaaaaaaaakjhasdfasdgoaaaalkjadslkjgoa aa lkahjga;lskdhgalskhdgaaaaa'
(Pdb) inds  = find_indicies(x, 'a')
(Pdb) inds
[(0, 2), (4, 5), (9, 9), (13, 20), (24, 24), (28, 28), (33, 36), (40, 40), (48, 48), (50, 51), (55, 55), (59, 59), (67, 67), (74, 78)]