Unable to use a tuple as a dictionary key?

8.3k views Asked by At

The code is a little complex, sorry. Please focus on the parallel_p function. Although sign is a tuple, Python complains:

if sign in hashtable and gives a TypeError. Why is sign a numpy.ndarray rather than a tuple? I created it as a tuple.

p_dist = dict()

def parallel_prog(hashtable):
    def wrapper(func):
        if parallel_programming:
            @wraps(func)
            def parallel_p(*args, **kwargs):
                sign = tuple(args) 
                print type(sign)
                if sign in hashtable:
                    r = hashtable[sign] # hit.
                    return r
                r = func(*args, **kwargs)
                hashtable[tuple(sign)] = r # register for future hits.
                return r
            return parallel_p
        else:
            return func
    return wrapper


@parallel_prog(p_dist)
def euclidean(a, b, signature=None):

    val = np.sqrt(np.add.reduce((a - b)**2))
    return val

IN TEST MODULE

class similartyTest(unittest.TestCase):

    def setUp(self):
        self.t = np.array([1,2,3,4])
        self.t1 = np.array([0, 0, 0, 0])
        self.t2 = np.array([4,3,2,1])
        self.h1 = [1,0,0,1,1]
        self.h2 = [1,1,1,1,1]

    def test_euclidean(self):
        self.failUnless(euclidean(self.t, self.t) == 0)
        self.failUnless(euclidean(self.t, self.t1) == sqrt(30))

OUTPUT:

<type 'tuple'>`

TypeError: unhashable type: 'numpy.ndarray`
2

There are 2 answers

1
Sven Marnach On

Not every tuple is hashable. A tuple containing non-hashable items is not hashable:

>>> x = ([], [])
>>> hash(x)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: unhashable type: 'list'

Your tuple obviously contains a NumPy array, which is as far from being hashable as a type can be.

0
X.Arthur On

Tuples are hashable only when they hold hashable references.

t1 = (7, 'A')
t2 = (7, 'A', {1, 2, 3})

t1 is hashable but t2 is not.

The link below gives a more detailed explanation: https://www.quora.com/Are-tuples-hashable-in-Python/answer/Luciano-Ramalho?srid=T4aW