String compression using repeated chars count
<p>I'm going through Cracking the Code and there is this question where they ask to write a method for string compression so:</p>

<pre><code>aabbccccaa 
</code></pre>

<p>Would become: </p>

<pre><code>a2b1c4a2
</code></pre>

<p>I came up with this:</p>

<pre><code>''.join(y+str.count(y) for y in set(str))
</code></pre>

<p>But my output was: </p>

<pre><code>a5c4b1
</code></pre>

<p>Could someone point me in the clean direction?</p>

<p>Sorry for bad edits, I'm on a cellphone</p>

<p>You could use <a href="https://docs.python.org/3/library/itertools.html#itertools.groupby"><code>groupby</code></a> to do the work for you:</p>

<pre><code>&gt;&gt;&gt; from itertools import groupby
&gt;&gt;&gt; s = 'aabbccccaa'
&gt;&gt;&gt; ''.join(k + str(sum(1 for x in g)) for k, g in groupby(s))
'a2b2c4a2'
</code></pre>

python