0

I am trying to figure out how to count how many times one string appears in another string. My code that I'm trying to use and have played around with has not worked so far.

needle = input()
haystack = input()

count = 0

for needle in haystack:
   count += 1

print(count)  

My expected result if the input for haystack is 'sesses' and needle is 'ses' would be to print out 2. (ses is in haystack twice)

  • `for needle in haystack` is actually starting a loop for all items in 'sessess' which returns 6. – Anton vBR Nov 01 '17 at 20:00
  • 1
    Do you want to count overlapping instances? – yinnonsanders Nov 01 '17 at 20:00
  • I found this to be the most simple indeed: `import regex as re; len(re.findall('sses','assesses',overlapped=True))` - see this: https://stackoverflow.com/questions/5616822/python-regex-find-all-overlapping-matches – Anton vBR Nov 01 '17 at 20:08

5 Answers5

1
'sesses'.count('ses') will give you the answer
kar
  • 198
  • 1
  • 8
  • This unfortunately does not work. I've tried using haystack.count(needle) but the result it gives me is 1 when it should be 2. (For the input assesses as haystack and sses for needle) –  Nov 01 '17 at 20:01
1
for needle in haystack:
    count += 1

The reason this doesn't work is that "for x in y" is iterating over values in y, and /placing/ them into x; it does not read from x. So in your code example, you are iterating over haystack (which, in Python, means looping through each letter), and then placing the value (each letter) into needle. Which is not what you want.

There's no built-in iterator to give you what you want -- an iterator of the occurrences of another string. haystack.count(needle) will give you the desired answer; other than that, you could also use haystack.find to find an occurrence starting at a given point, and keep track of how far you are in the string yourself:

index = haystack.find(needle)
while index >= 0:
    count += 1
    index = haystack.find(needle, index + 1)

Note that this will give a different answer than haystack.count: haystack.count will never count a letter twice, whereas the above will. So "aaaaaa".count("aaa") will return 2 (finding "aaaaaa" and "aaaaaa"), but the above code would return 4, because it would find "aaaaaa", "aaaaaa", "aaaaaa", and "aaaaaa".

Magua
  • 258
  • 1
  • 9
1

Use count()

needle = input()
haystack = input()
print haystack.count(needle)
1

Try this :)

characters = 'ses'
word = 'sesses'
chunkSize = 1

count = 0
while chunkSize < len(word):
    for i in range(len(word) - chunkSize + 1):
        if word[i:chunkSize+i] == characters:
            count += 1
    chunkSize += 1
print(count)
Luke McPuke
  • 354
  • 1
  • 2
  • 12
0

This should work:

a='seses'
b='se'
print a.count(b)
>>2
theBrainyGeek
  • 584
  • 1
  • 6
  • 17
  • This doesn't work unfortunately. I tried the count function but when I try that I don't get the right number. For instance of haystack = assesses and needle = sses the result should be 2, but it prints out 1. –  Nov 01 '17 at 20:00
  • You want the third 's' in assesses to be counted twice? – theBrainyGeek Nov 01 '17 at 20:01