これは、リンクされた質問に対するブランドンの回答に基づく実装です。
def MIT(full_string, substring):
results = []
sub_len = len(substring)
for i in range(len(full_string)):
# range returns a list of values from 0 to (len(full_string) - 1)
if full_string[i:i+sub_len] == substring:
# this is slice notation;
# it means take characters i up to (but not including) i
# + the length of th substring
results.append(i)
return results
full_string = "bobBOBBobBoj"
lower_substring = "bob"
upper_substring = "BOB"
lower_occurrence_array = MIT(full_string, lower_substring)
upper_occurrence_array = MIT(full_string, upper_substring)
number_of_lower_occurrences = len(lower_occurrence_array)
number_of_upper_occurrences = len(upper_occurrence_array)
number_of_occurrences = number_of_lower_occurrences + number_of_upper_occurrences
print ("Number of times bob occurs is: %s" %number_of_occurrences)
結果は
ボブの出現回数:2回
主な要点はすでにポールの答えにありますが、これが役立つことを願っています.