好吧,这可能会更快,因为它可以在C中进行比较:
def occurrences(string, sub): count = start = 0 while True: start = string.find(sub, start) + 1 if start > 0: count+=1 else: return count

好吧,这可能会更快,因为它可以在C中进行比较:
def occurrences(string, sub): count = start = 0 while True: start = string.find(sub, start) + 1 if start > 0: count+=1 else: return count