0

I have the following sentence -

Sammy likes to swim in the ocean, likes to spin servers, and likes to smile.

I want to find the starting index number of the second occurrence of the word likes

But I don't want to use a starting index number or range as parameter of str.find() function.

If possible I want to avoid regular expression as it is difficult to understand for starters.

NOTE: It is not mandatory to use str.find() function. I just want to know whether it is possible without giving starting index or range as parameter.

puregeek
  • 175
  • 3
  • 9
  • 2
    May I ask why you do not want to use a starting index number or range in `str.find()`? – 1313e Jan 03 '18 at 12:56
  • I am not saying that you have to use str.find() I just want to know if it is possible without starting index – puregeek Jan 03 '18 at 12:57
  • 1
    So you just want to find the starting index of the second occurrence of the word "likes" ? If so then you can assign this string to an array of characters and loop through it until you find the word "likes". Keep a counter for this word and when this hits 2 you can send back the index. I can clarify this more and provide a better understanding of what i am saying if this is what you want? – Junaid Shirwani Jan 03 '18 at 12:59
  • Are you saying thsy you want to use str.find() without giving it an index number? – Junaid Shirwani Jan 03 '18 at 13:00
  • @JunaidShirwani Yes this is what I want, thanks – puregeek Jan 03 '18 at 13:00
  • I mean the method you said, the loop method, that I want, thanks – puregeek Jan 03 '18 at 13:00
  • Possible duplicate of [Find all occurrences of a substring in Python](https://stackoverflow.com/questions/4664850/find-all-occurrences-of-a-substring-in-python) – Hannu Jan 03 '18 at 13:01
  • @Hannu regular expression is very difficult for newbies, the method as mentioned by Junaid is helpful – puregeek Jan 03 '18 at 13:03
  • 1
    you will have an outer loop and an inner loop. In the inner loop you will compare characters until the word likes is found. If found you increase the counter else you keep looking . – Junaid Shirwani Jan 03 '18 at 13:03
  • Can I politely ask what is the point of this question. As soon as completely acceptable and working answers are given, you modify the question and explain you do not actually want any of these answers, but something else instead. – Hannu Jan 03 '18 at 13:05
  • @Hannu I am sorry for the confusion, I am studying str.find() in Python. But it has the limitation of using starting index for second occurence onwards. So I thought if any alternative is available. But I am just starting, so I don't know about regex – puregeek Jan 03 '18 at 13:08
  • 2
    It would be an excellent opportunity to learn... this is not a complex regex exercise. There could be a performance issue with brute force loops compared to regular expressions when your string gains length, as there is absolutely no optimisation in that. – Hannu Jan 03 '18 at 13:13
  • @Hannu can you please cite a good online tutorial for learning regex? thanks – puregeek Jan 03 '18 at 13:17
  • I have used this myself: https://regexone.com/references/python There is also a link to regex tester. I used it to practice and see what goes wrong. – Hannu Jan 03 '18 at 13:20

3 Answers3

3

Using regular expressions and start and end of the second capturing group:

import re
s = 'Sammy likes to swim in the ocean, likes to spin servers, and likes to smile.'
m = re.match(r'.*(likes).*(likes).*', s)
m.start(2)  # start of second capturing group
# 61
m.end(2)  # end of second capturing group
# 66
s[m.start(2), m.end(2)]
# 'likes'
user2390182
  • 72,016
  • 6
  • 67
  • 89
1

What about using regex?

import re

string = 'Sammy likes to swim in the ocean, likes to spin servers, and likes to smile.'
pattern = 'likes'
likes = [(m.start(0), m.end(0)) for m in re.finditer(pattern, string)]
# [(6, 11), (34, 39), (61, 66)]

likes[1][0] has what you want

Iago Díaz
  • 368
  • 1
  • 2
  • 10
1

Normal looping method:

string = "Sammy likes to swim in the ocean, likes to spin servers, and likes to smile."
word = "likes"
indexes = []
for i in range(len(string)):
    if string[i:i+len(word)] == word:
        indexes.append(i)
print(indexes[1]) # Second location, outputs: 34

List comprehension:

[i for i in range(len(string)) if string[i:i+len(word)] == word][1] # Outputs: 34
Omar Einea
  • 2,478
  • 7
  • 23
  • 35