Count the number of occurrences of a given item in a (sorted) list?
<p>I'm asked to create a method that returns the number of occurrences of a given item in a list. I know how to write code to find a specific item, but how can I code it to where it counts the number of occurrences of a random item. </p>

<p>For example if I have a <code>list [4, 6 4, 3, 6, 4, 9]</code> and I type something like</p>

<p><code>s1.count(4)</code>, it should <code>return 3</code> or  <code>s1.count(6)</code> should <code>return 2</code>. </p>

<p>I'm not allowed to use and built-in functions though.</p>

<p>In a recent assignment, I was asked to count the number of occurrences that <code>sub string</code> "ou" appeared in a given string, and I coded it </p>

<pre><code>if len(astr) &lt; 2:
    return 0
else:
    return (astr[:2] == "ou")+ count_pattern(astr[1:])
</code></pre>

<p>Would something like this work??</p>

<pre><code>def count(self, item):
    num=0
    for i in self.s_list:
        if i in self.s_list:
            num[i] +=1
def __str__(self):
    return str(self.s_list)
</code></pre>
