Python - Alternative for using numpy array as key in dictionary
<p>I'm pretty new to Python numpy. I was attempted to use numpy array as the key in dictionary in one of my functions and then been told by Python interpreter that numpy array is not hashable. I've just found out that one way to work this issue around is to use <code>repr()</code> function to convert numpy array to a string but it seems very expensive. Is there any better way to achieve same effect?</p>

<p>Update: I could create a new class to contain the numpy array, which seems to be right way to achieve what I want. Just wondering if there is any better method?</p>

<p>update 2: Using a class to contain data in the array and then override <code>__hash__</code> function is acceptable, however, I'd prefer the solution provided by @hpaulj. Convert the <code>array/list</code> to a <code>tuple</code> fits my need in a better way as it does not require an additional class. </p>

<p>After done some researches and reading through all comments. I think I've known the answer to my own question so I'd just write them down.</p>

<ol>
<li>Write a class to contain the data in the <code>array</code> and then override <code>__hash__</code> function to amend the way how it is <strong>hashed</strong> as mentioned by <a href="http://stackoverflow.com/users/3051961/zdar">ZdaR</a></li>
<li>Convert this <code>array</code> to a <code>tuple</code>, which makes the list <strong>hashable</strong> instantaneously.Thanks to <a href="http://stackoverflow.com/users/901925/hpaulj">hpaulj</a></li>
</ol>

<p>I'd prefer method No.2 because it fits my need better, as well as simpler. However, using a class might bring some additional benefits so it could also be useful.</p>

python arrays numpy dictionary