<div dir="ltr"><br><div class="gmail_extra"><br><br><div class="gmail_quote">On Sat, Dec 29, 2012 at 7:26 PM, Tim Chase <span dir="ltr"><<a href="mailto:python.list@tim.thechases.com" target="_blank">python.list@tim.thechases.com</a>></span> wrote:<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div class="im">On 12/29/12 15:40, Mitya Sirenef wrote:<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
      >>> w = [1,2,3,1,2,4,4,5,6,1]<br>
      >>> s = set(w)<br>
      >>> s<br>
      set([1, 2, 3, 4, 5, 6])<br>
      >>> {x:w.count(x) for x in s}<br>
      {1: 3, 2: 2, 3: 1, 4: 2, 5: 1, 6: 1}<br>
</blockquote>
<br>
Indeed, this is much better -- I didn't think of it..<br>
</blockquote>
<br></div>
Except that you're still overwhelmed by iterating over every element in "w" for every distinct element.  So you've gone from O(N**2) to O(k*N).<br>
<br>
The cleanest way to write it (IMHO) is MRAB's<div class="im"><br>
<br>
 >>> w = [1, 2, 3, 1, 2, 4, 4, 5, 6, 1]<br></div>
 >>> from collections import Counter<br>
 >>> results = dict(Counter(w))<br>
<br>
which should gather all the statistics in one single pass across "w" making it O(N), and it's Pythonically readable.<br>
<br>
-tkc<span class="HOEnZb"><font color="#888888"><br>
<br></font></span></blockquote><div>I like this too.  I haven't learned about collections module yet.  Thanks for the pointer <br></div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
<span class="HOEnZb"><font color="#888888">
<br>
<br>
-- <br>
<a href="http://mail.python.org/mailman/listinfo/python-list" target="_blank">http://mail.python.org/<u></u>mailman/listinfo/python-list</a><br>
</font></span></blockquote></div><br><br clear="all"><br>-- <br>Joel Goldstick<br>
</div></div>