Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unable to use a tuple as a dictionary key?

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`
like image 789
Baskaya Avatar asked Nov 09 '11 22:11

Baskaya


1 Answers

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.

like image 196
Sven Marnach Avatar answered Sep 21 '22 01:09

Sven Marnach