0

I need to count non-unique sequences going sequentially. For example: "aabcccjaaa" is "21313". But my code do not count the last string. In checks, it goes to the last "else" and must add the last "a" as a unit to the result. What could be the problem? And maybe someone knows a solution instead of mine using standard libraries?

a = "assdddfghttyuuujssa"
b = ''
c = 1
d = []
for item in a:
    if item == b:
        c += 1
    elif b == '':
        c = 1
    else:
        d.append(c)
        c = 1
    b = item

print(d)

I tried to add output of unique words on each iteration of the loop, however it still doesn't show why the last "append" doesn't add "1" to the result.

Djery SC
  • 13
  • 2
  • Ever heard of **regex** in python? – Sivadithiyan Nov 05 '22 at 12:12
  • Does this answer your question? "[Counting consecutive characters in a string](/q/13197668/90527)", "[Count consecutive letters](/q/66023254/90527)", … – outis Nov 06 '22 at 05:43
  • As per the [site guidelines](/help/how-to-ask) in the [help], please [search](/help/searching) before posting. See also "[How much research effort is expected of Stack Overflow users?](https://meta.stackoverflow.com/q/261592/90527)" – outis Nov 06 '22 at 05:44

2 Answers2

0

Your code appends a number to d when the character is different from the previous one. It adds the number associated with the run of previous characters. So the last run never gets added, because there is no character after the last number.

A simple fix would be to add

if b != '':
    d.append(c)

... after the loop is finished. (Can you tell why I added that check for b?)

Of course, there is a simpler way, using the standard library:

from itertools import groupby

d = [len(list(same)) for _, same in groupby(a)]
Jasmijn
  • 9,370
  • 2
  • 29
  • 43
  • Thanks a lot! The solution turned out to be really simple. And I'm glad that you helped me get off the ground. Thank you for your time! – Djery SC Nov 05 '22 at 12:37
0

Your implementation works decently. You're just getting this error because you don't add the last item of c when the for-loop ends. Here is your fix:

a = "assdddfghttyuuujssa"
b = ''
c = 1
d = []

for item in a:
    
    if item == b:
        c += 1
    elif b == '':
        c = 1
    else:
        d.append(c)
        c = 1
    b = item

d.append(c) # just added this line
print(d)

I'd recommend in the future to use better naming descriptions for your variable names. This code is quite difficult to unpack and understand since everything is just called a, b, c, ... . Hope this helps!