Bob Counter in Python

后端 未结 8 1442
名媛妹妹
名媛妹妹 2021-01-28 11:25

Using Python 2.7, I was attempting to count the number of occurances of \'bob\' in the phrase \'bobbbobobboobobookobobbobbboj.\' To do this, I wrote the code below:



        
8条回答
  •  遇见更好的自我
    2021-01-28 11:46

    >>> s = 'bobbbobobboobobookobobbobbboj'
    >>> term = 'bob'
    sum(1 for i, j in enumerate(s) if s[i:i+len(term)] == term)
    6
    

提交回复
热议问题