繁体   English   中英

如何找到一个单词在给定字符串中连续重复的最大次数?

[英]How to find the largest number of times a word is repeated consecutively in a given string?

好的,所以这是一个令人困惑的问题,我会尝试以最好的方式来表达它。

我试图找出一种方法,可以在 Python 的字符串中找到单词的最大连续重复

例如,假设我要查找的单词是“apple”,字符串是:“ apple banorangeorangeorangebanana appleapple orangeapple appleappleappleapple ”。 这里,单词“apple”的最大连续重复次数是 3。

我尝试了多种方法来查找重复字符,例如:

word="100011010" #word = "1"
count=1
length=""
if len(word)>1:
    for i in range(1,len(word)):
       if word[i-1]==word[i]:
          count+=1
       else :
           length += word[i-1]+" repeats "+str(count)+", "
           count=1
    length += ("and "+word[i]+" repeats "+str(count))
else:
    i=0
    length += ("and "+word[i]+" repeats "+str(count))
print (length)

但这适用于整数而不是单词。 它还输出字符一般重复的次数,但不识别最大的连续重复。 我希望这是有道理的。 我的大脑有点乱,所以如果我绊倒了,我道歉

这是我想出的一个解决方案,我相信它可以解决您的问题。 如果您花更多时间解决我鼓励的问题,几乎可以肯定有一种更简单/更快的方法。

import re

search_string = "applebananaorangeorangeorangebananaappleappleorangeappleappleappleapple"
search_term = "apple"


def search_for_term(search_string, search_term):
    #split string into array on search_term
    #keeps search term in array unlike normal string split
    split_string = re.split(f'({search_term})', search_string)

    #remove unnecessary characters
    split_string = list(filter(lambda x: x != "", split_string))

    #enumerate string and filter out instances that aren't the search term
    enum_string = list(filter(lambda x: x[1] == search_term, enumerate(split_string)))

    #loop through each of the items in the enumerated list and save to the current chain
    #once a chain brakes i.e. the next element is not in order append the current_chain to 
    #the chains list and start over
    chains = []
    current_chain = []
    for idx, val in enum_string:
        if len(current_chain) == 0:
            current_chain.append(idx)
        elif idx == current_chain[-1] + 1:
            current_chain.append(idx)
        else:
            chains.append(current_chain)
            current_chain = [idx]
        print(chains, current_chain)    

    #append anything leftover in the current_chain list to the chains list
    if len(current_chain) > 0:
        chains.append(current_chain)
        del current_chain

    #find the max length nested list in the chains list and return it
    max_length = max(map(len, chains)) 
    return max_length


max_length = search_for_term(search_string, search_term)    
print(max_length)

这是我将如何做到这一点。 首先在randString中检查'apple',然后检查'appleapple',然后是'appleappleapple',依此类推,直到搜索结果为空。 跟踪迭代计数和瞧。

randString = "applebananaorangeorangeorangebananaappleappleorangeappleappleappleapple"
find = input('type in word to search for: ')


def consecutive():
    count =0

    for i in range(len(randString)):
        count +=1
        seachword = [find*count]
        check = [item for item in seachword if item in randString]

        if len(check) != 0:
            continue

        else:
            # Need to remove 1 from the final count.
            print (find, ":", count -1)
            break

consecutive()

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM