stefankoegl / kdtree

A Python implementation of a kd-tree
ISC License
365 stars 118 forks source link

'list' is unhashable #12

Closed kerzol closed 10 years ago

kerzol commented 10 years ago

It seems that we cannot store lists in kdtree. See what i have, when i try to find a nearest neighbours in the kdtree with the list inside.

In [2]: tr = kdtree.create(dimensions=3)

In [3]: tr.add([1,2,3])

In [4]: tr.search_knn( [1,2,3] ,10)
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-4-e685e1032c19> in <module>()
----> 1 tr.search_knn( [1,2,3] ,10)

/kdtree.py in search_knn(self, point, k)
    416 
    417         # the nodes do not keep a reference to their parents
--> 418         parents = {current: None}
    419 
    420         # go down the tree as we would for inserting

/kdtree.py in __hash__(self)
    187 
    188     def __hash__(self):
--> 189         return hash(self.data)
    190 
    191 

TypeError: unhashable type: 'list'
stefankoegl commented 10 years ago

Thanks for reporting this. Since the latest version kdtree is relying on the points to be hash-able, which breaks lists.

I've provided a fix in be6e639b4f71e279cbe6f5690bb03c1deefcb87b, which I just released as 0.8.

kerzol commented 10 years ago

that's cool! Thanks.