3

I'm trying to figure out how I can count the number of letters in a string that occur 3 times. The string is from raw_input().

For example, if my input is:

abceeedtyooo 

The output should be: 2

This is my current code:

print 'Enter String:'
x = str(raw_input (""))

print  x.count(x[0]*3)
jfs
  • 399,953
  • 195
  • 994
  • 1,670
  • You've explained what you're trying to do, but not what your specific question is. Are you getting an exception? Bad results? Exploding computer screen? – skrrgwasme Aug 15 '14 at 20:26
  • Is this a homework problem? – pcurry Aug 15 '14 at 20:29
  • We can still help you if it is a homework problem, but you've got to give us some details to walk you through to a solution instead of just giving you the answer. – skrrgwasme Aug 15 '14 at 20:31
  • Do you want to count *consecutive* duplicates as in your example? – jfs Aug 15 '14 at 21:24
  • yes thats what im tyring to do count how many 3 consecutive duplicates in the string – Paul Van Doren Aug 16 '14 at 00:20
  • related: [Python - how to remove duplicates only if consecutive in a string?](http://stackoverflow.com/q/11460855/4279) – jfs Aug 16 '14 at 22:39

3 Answers3

3

To count number of consecutive duplicate letters that occur exactly 3 times:

>>> from itertools import groupby
>>> sum(len(list(dups)) == 3 for _, dups in groupby("abceeedtyooo"))
2
jfs
  • 399,953
  • 195
  • 994
  • 1,670
2

To count the chars in the string, you can use collections.Counter:

>>> from collections import Counter
>>> counter = Counter("abceeedtyooo")
>>> print(counter)
Counter({'e': 3, 'o': 3, 'a': 1, 'd': 1, 'y': 1, 'c': 1, 'b': 1, 't': 1})

Then you can filter the result as follows:

>>> result = [char for char in counter if counter[char] == 3]
>>> print(result)
['e', 'o']

If you want to match consecutive characters only, you can use regex (cf. re):

>>> import re
>>> result = re.findall(r"(.)\1\1", "abceeedtyooo")
>>> print(result)
['e', 'o']
>>> result = re.findall(r"(.)\1\1", "abcaaa")
>>> print(result)
['a']

This will also match if the same character appears three consecutive times multiple times (e.g. on "aaabcaaa", it will match 'a' twice). Matches are non-overlapping, so on "aaaa" it will only match once, but on "aaaaaa" it will match twice. Should you not want multiple matches on consecutive strings, modify the regex to r"(.)\1\1(?!\1)". To avoid matching any chars that appear more than 3 consecutive times, use (.)(?<!(?=\1)..)\1{2}(?!\1). This works around a problem with Python's regex module that cannot handle (?<!\1).

hlt
  • 6,219
  • 3
  • 23
  • 43
  • i used this and yes it prints out the letters with 3 consecutive duplicates, so how do i count the result in this case its "e" and 'o' i want to count them that with give me the output of '2' so for example i have aaabbbccc: the count will be: 3 – Paul Van Doren Aug 16 '14 at 01:16
  • Use `len` to find the length of the result list: `print(len(result))` should output `2`. – hlt Aug 16 '14 at 01:17
  • why is it that there are some cases of letter combination that it fails to count for example my string is:abcaaa the result is 0 – Paul Van Doren Aug 16 '14 at 01:38
  • Because `a` appears 4 times. I'm going to add something for 3 *consecutive* chars... – hlt Aug 16 '14 at 01:39
  • sorry forgot to confirm this :) your awesome, thanks! – Paul Van Doren Aug 21 '14 at 09:37
1

We can count the chars in the string through 'for' loop

s="abbbaaaaaaccdaaab"
st=[]
count=0
for i in set(s):
    print(i+str(s.count(i)),end='')

Output: a10c2b4d1

innicoder
  • 2,612
  • 3
  • 14
  • 29