簡體   English   中英

如何僅使用find和replace查找並計算字符串中所有子字符串的出現次數?

[英]How do I find and count all the occurrences of a substring in a string using only find and replace?

該條目需要更低,最后程序必須打印出現次數。 例如mem。

smthing = str(input())
if (smthing == smthing.lower()):
    smthing.find(mem)

我完全沉溺於此,所以我無法走遠。

我忘了告訴我不能使用計數或列表。

怎么樣的

string = "hello world hello".lower()
replace = "hello".lower()
count = 0

while string.find(replace) != -1:
    string = string.replace(replace, "", 1)
    count += 1

print count
# => Output
# => 2

要處理重疊的字符串,而不是替換整個子字符串,我們可以只替換單個字符,最好是第一個,從原始字符串replace[0]

string = "boboobobobobob".lower()
replace = "bob".lower()
count = 0 

while string.find(replace) != -1:
    string = string.replace(replace[0], "", 1)
    count += 1

print count
# Output
# => 6

如果你有重疊的字符串,你需要一次更換一個字符:

sub = "bob"
smthing = input()
count = 0

for i in iter(lambda: smthing.find(sub), -1):
    smthing = smthing.replace(sub[0], "", 1)
    count += 1
print(count)

所以對於boboobobobobob你會得到6而不是3。

如果您不能使用計數但可以使用其中一個或另一個,則可以單獨使用替換,但這不包括重疊:

print((len(smthing) - len(smthing.replace(sub,""))) / len(sub))

試試這段代碼:

smthing = "blablabla"
mem = "bl"
count = 0
if (smthing == smthing.lower()):
    bkpsmthing = smthing # maybe you'll need it later
    while (smthing.find(mem) > -1):
        smthing = smthing.replace(mem, '', 1)
        count += 1
print count

它使用str.find在沒有找到任何內容時返回-1的事實,或者指向要停止的最低條目的索引:

返回s中找到子字符串sub的最低索引,使得sub完全包含在s [start:end]中。 失敗時返回-1。

它還使用了str.replace可以通過使用maxreplace參數( maxreplace )刪除單個條目(最低)的maxreplace 這樣我們就會不斷刪除我們計算過的條目:

...如果給出了可選參數maxreplace,則替換第一個maxreplace事件。

該過程可以這樣描述:

find "bl" in "blablabla" -> found at index 0
replace "bl" in "blablabla" -> gives "ablabla"
find "bl" in "ablabla" -> found at index 1
replace "bl" in "ablabla" -> gives "aabla"
find "bl" in "aabla" -> found at index 2
replace "bl" in "aabla" -> gives "aaa"
find "bl" in "aaa" -> not found, returns -1
stop

要對count變量執行相同的操作,請使用這個簡單的遞歸(確保在使用my_count之前驗證字符串是小寫的):

def my_count(my_string, my_substring):
    if my_string.find(my_substring) > -1:
        new_string = my_string.replace(my_substring, '', 1)
        return my_count(new_string, my_substring) + 1
    return 0

輸出:

>>> my_count("blablabla", "bl")
3

遞歸展開如下:

my_count("blablabla", "bl") =
= my_count("ablabla", "bl") + 1 =
= my_count("aabla", "bl") + 1 + 1 =
= my_count("aaa", "bl") + 1 + 1 + 1 =
= 0 + 1 + 1 + 1 = 3

你沒有; 你需要額外的工具,可能只需要基本算法。 例如,如果使用不同長度的子字符串替換子字符串,則可以將結果的長度與原始字符串進行比較,以計算出現的次數。 另一種選擇是使用start參數來查找其他出現的情況。 我很想知道你嘗試了什么; 您顯示的代碼不會產生任何結果。

對於記錄,您可以使用string.replace()執行此操作:

smthing = str(input())  
word='mem'
x=0

while word in smthing:
    smthing=smthing.replace(word,'',1)
    x+=1

print x

演示:

>>> 
memory memory memory
3

您的問題 - 查找和計算字符串中所有出現的子字符串 - 不需要按定義進行替換。

您應該能夠只接受輸入,強制降低輸入,並使用count方法查找子串。

num_substrings = input().lower().count(substring)

我還沒有看到這種方法,只是循環查找計數出現:

a='this is a test aaaa'
count =0
loc = a.find('is')
while loc > -1:
    count += 1
    loc = a.find('is',loc+1)
print count -1 

暫無
暫無

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

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