1

I have a string

string = 'AAA'

When using the string.count('A') the output is equal to 3 and if it is string.count('AA') the output is equal to 1

However, there are 2 'AA's in the string.

Is there any method to count repeated string like above by using dictionary function?

I'd like to hear your helpful suggestions.

Thank you all in advance.

Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
Karyo
  • 372
  • 2
  • 4
  • 21
  • Can you add you code? – Kobi K Oct 11 '13 at 07:33
  • You are trying to look for overlapping strings; why do you want to use a dictionary function? – Martijn Pieters Oct 11 '13 at 07:35
  • 4
    possible duplicate of [string count with overlapping occurances](http://stackoverflow.com/questions/2970520/string-count-with-overlapping-occurances) – TerryA Oct 11 '13 at 07:36
  • Thank you all, I used regrex instead, and also found doining it by the dictionary. My problem was that if use Dict it only counted single 'A', but position of characters can be manipulated to count 'AA'. – Karyo Oct 16 '13 at 02:01

3 Answers3

1

The problem is Count return the number of (non-overlapping) occurrences of substring sub in string.

try this as you can see at this post:

def occurrences(string, sub):
    count = start = 0
    while True:
        start = string.find(sub, start) + 1
        if start > 0:
            count+=1
        else:
            return count
Community
  • 1
  • 1
Kobi K
  • 7,743
  • 6
  • 42
  • 86
  • 4
    You may want to at least give a reference to [this](http://stackoverflow.com/a/2970542/1971805), which is exactly the same as your answer :/ – TerryA Oct 11 '13 at 07:38
  • You are right! i had the same problem 1 month ago and i took it from this post http://stackoverflow.com/questions/2970520/string-count-with-overlapping-occurances/2970542#2970542 for the answer i just uploaded my code and copied to here. credit for the method is for @Jochen Ritzel – Kobi K Oct 11 '13 at 07:42
  • @KobiK FYI, according to the license, [attribution is required when using code from SO](http://meta.stackexchange.com/questions/25956/what-is-up-with-the-source-code-license-on-stack-overflow/25957#25957). IANAL, but you can do this with a comment alongside the code, that way you also would have remembered the source :). Still, best to rewrite snippets of SO code than copying them verbatim. – Dennis Oct 11 '13 at 07:59
  • @Dennis Thank you for the comment, i had no idea! i never asked for anything when i answered a post, i think your suggestion is a good practice and i'll use it, generally i always give a link to posts and docs, this time just didn't remembered. – Kobi K Oct 11 '13 at 08:07
1

Alternative for "not huge" strings

>>> s, sub = 'AAA', 'AA'
>>> sum(s[x:].startswith(sub) for x in range(len(s)))
2

I find this a little more readable.

Marco Mariani
  • 13,556
  • 6
  • 39
  • 55
-1

Yeah! You can use a dictionary

def count(input):

    a = {}

    for i in input:
        a[i] = a.get(i,0)+ 1
    return a

print(count('AA')) #Would return 2
Rolf of Saxony
  • 21,661
  • 5
  • 39
  • 60
getflag
  • 53
  • 1
  • 2
  • 10
  • I love the method used to build the dictionary for a character count but that is all that this code does. It counts the number of each character passed in the string. The OP is looking for a count that takes into account overlapping characters. If you pass "AAA" into this code it returns 3 (a character count) as opposed to 2 (the number of occurrences of "AA") – Rolf of Saxony Jun 18 '17 at 09:01