簡體   English   中英

這是在python中找出特定名稱在字符串中連續多少次的正確方法嗎

[英]Is this a correct way in python to find out the that a specific name is in the string for how many max no of times continously

用於查找連續次數的特定名稱的程序。 例如,'yaweryaweryaweraliyawer' 此處的 yawer 最多連續 3 次。

##Created a string to checking for a specific name successive no of times.   

string = 'yaweraliyaweryaweryawer'
##this is the name to find

name = 'yawer'

##max no of times name is in the string 
count = string.count(name)
##reverse loop for checking 
for i in range(count,0,-1):
      if name * count in string:
            print(f"No of successive times {name} is in string is : {count})
            break;

你快到了,只需用計數器“i”刪除名稱*計數

像這樣:

for i in range(count,0,-1):
      if name * i in string:
            print(f"No of successive times {name} is in string is : {i}")
            break;

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM