Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Conclusion:</p> <ul> <li>If all items are hashable and is at least Python 2.7, the best solution is above with <strong><a href="https://stackoverflow.com/a/10247854/448474">collections.Counter</a></strong>.</li> <li>If some items are not hashable or Python 2.7+ is not present, then the solution <code>groupby(sorted(..))</code> is very good under conditions <ul> <li>not combine str and unicode or</li> <li>not use any type with the name placed aphabetically between "str" and "unicode". (typically "tuple" or "type")</li> </ul></li> <li>If data are unhashable and of mixed types, nothing above can be used and then the best is to use: <ul> <li><code>Counter(map(pickled.dumps, data))</code> instead of <code>Counter(data)</code> and finally unpickle it or</li> <li><code>groupby(sorted(data, key=pickled.dumps))</code> if unpickling is undesirable or no python 2.7</li> </ul></li> <li>A "<a href="https://stackoverflow.com/a/5751556/448474">naive solution</a>" discussed below can be better than pickling for very small number of items approximately less than 300.</li> </ul> <p><strong>All other</strong> solutions in other questions <strong>are worse</strong> currently.</p> <p>Notes:</p> <ul> <li>It looks that the class Counter can be copied&amp;pasted to lower Python versions.</li> <li>A "<a href="https://stackoverflow.com/a/5751556/448474">naive solution</a>" that searches every item in whole data can be used for very small number of items. It has the advantage that does not require hashable data and does not depend on transitivity of default "&lt;" operator, but for more than 25 hashable items is better the counter and for more than 300 unhashable "wild" items is better the counter on pickled items.</li> </ul> <p>I thought about presorting items by type or extend them by hash for hashable items, it helps currently, but it is not a safe solution because the same problem would be with "&lt;" operator insite lists, tuples etc.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload