给定某一长字符串s='xxxxbobobxxxx',试判断ss='bob'在s中出现的次数。结果为2
2 回答
已采纳

Jeffacode
TA贡献1条经验 获得超1个赞
def frequency(s1, s2): count = 0 while s1: if s1.find(s2) == -1: break count += 1 s1 = s1[s1.find(s2) + len(s2):] return count if __name__ == '__main__': s1 = "xxxxbobobxxxx" s2 = "bob" print("%s occurs %d times in %s" % (s2, frequency(s1, s2), s1))

小萝卜腿
TA贡献51条经验 获得超20个赞
#已经解决了 def finds(s1, s2): i = 0 count = 0 while(i<len(s2) - len(s1) + 1): if(s1 == s2[i:i+3]): count+=1 i+=1 return count s1 = 'bob' s2 = 'azcbobobegghakl' print ("Number of times bob occurs is: "+str(finds(s1, s2)))
添加回答
举报
0/150
提交
取消