Counting duplicate characters
<p>I can't figure out why the last character in a string always gets omitted when I try the following:</p>

<pre><code>def duplicate_count(text):
    num = 0
    count = {}
    for char in text:
        print(count.items())
        if char in count.keys():
            count[char] += 1
        else:
            count [char] = 1
    for key in count:
        if count[key] == 1:
           num = 0
        else:
           num = count[key] - 1
    return (num)

char_s = 'abcde'
print (duplicate_count(char_s))
</code></pre>

<p>In my opinion this could use some more segmentation. In my opinion you should not be focused on counting duplicate letters; you should narrow the scope of your function to identifying which letters occur, and how many times they occur. This can be easily stored in a dictionary variable.</p>

<p>Example:</p>

<pre><code>def string_occurence (string):
    dict={}
    for char in string:
        dict [char]=string.count (char)
    return dict
</code></pre>

<p>You can also optimise this by creating a set of unique characters before checking the string:</p>

<pre><code>def string_occurence (string):
    char_set=set ([char for char in string])
    dict={}
    for char in char_set:
        dict [char]=string.count (char)
    return dict
</code></pre>

<p>This hinges on how you want your control flow to work, but I think the second function would work best for you.</p>

python string python-3.x