Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>However, there is a pitfall in both solutions. The reason is that it merges the values with the same hash. So, it depends on whether the used values may have the same hash. It is not that crazy comment as you may think (I was also surprised earlier), because Python hashes some values the special way. Try:</p> <pre><code>from collections import Counter def counter_way(x): return [k for k,v in Counter(x).iteritems() if v &gt; 1] def accepted_way(x): #accepted answer in the linked thread duplicates = set() found = set() for item in x: if item in found: duplicates.add(item) else: found.add(item) return duplicates a = ('x ', (1,), u'x') * 2 print 'The values:', a print 'Counter way duplicates:', counter_way(a) print 'Accepted way duplicates:', accepted_way(a) print '-' * 50 # Now the problematic values. a = 2 * (0, 1, True, False, 0.0, 1.0) print 'The values:', a print 'Counter way duplicates:', counter_way(a) print 'Accepted way duplicates:', accepted_way(a) </code></pre> <p>The 1, 1.0, and True have the same hash by definition, and similarly the 0, 0.0, and False. It prints the following on my console (think about the last two lines -- all the values should actually be duplicates):</p> <pre><code>c:\tmp\___python\hynekcer\so10247815&gt;python d.py The values: ('x ', (1,), u'x', 'x ', (1,), u'x') Counter way duplicates: [u'x', 'x ', (1,)] Accepted way duplicates: set([u'x', 'x ', (1,)]) -------------------------------------------------- The values: (0, 1, True, False, 0.0, 1.0, 0, 1, True, False, 0.0, 1.0) Counter way duplicates: [0, 1] Accepted way duplicates: set([False, True]) </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

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