Reputation: 13
Trying to do this:
ls = np.empty
ls =getColA()
rs = np.empty
rs=getColG()
x = dict(itertools.izip(ls,rs))
and getting this error:
TypeError: unhashable type: 'numpy.ndarray'
would appreciate if anyone could help me out.
Upvotes: 0
Views: 1754
Reputation: 231665
There several things that don't make sense
ls = np.empty
ls =getColA()
np.empty
is a function. You assign that to ls
; then you assign the result of getColA()
to ls
. That wipes out the first assignment. Do you think the first assignment defines the ls
variable as an array? That's not how Python works. Variables don't have a type.
x = dict(itertools.izip(ls,rs))
You didn't tell us what getColA()
returns, but the error indicates that it is an array. 1d? 2d?
This sort of dictionary building works with 1d arrays (I'm using PY3)
In [497]: dict(itertools.zip_longest(np.arange(3),np.arange(10,13)))
Out[497]: {0: 10, 1: 11, 2: 12}
but not with 2d
In [498]: dict(itertools.zip_longest(np.arange(6).reshape(2,3),np.ones((2,3))))
...
TypeError: unhashable type: 'numpy.ndarray'
That's because the zip
on 2d returns the rows of the arrays, and the rows themselves are arrays:
In [499]: list(itertools.zip_longest(np.arange(6).reshape(2,3),np.ones((2,3))
...: ))
Out[499]:
[(array([0, 1, 2]), array([ 1., 1., 1.])),
(array([3, 4, 5]), array([ 1., 1., 1.]))]
Why do you want to use an array as dictionary key?
Upvotes: 0
Reputation: 2174
A dict
must have the key set as a hashable type.
You are trying to create a type using a non-hashable type (numpy.ndarray).
This is why you get this error.
In you case, you could do:
x = dict(itertools.izip(tuple(ls),rs))
Upvotes: 0
Reputation: 107347
When you are using itertools.izip(ls,rs)
the first column will be contain numpy arrays which are not hashable to stand as dictionary keys.
You have to convert them to a hashable iterable like tuple
instead. Also if you want to concatenate two numpy arrays along their second axis it's better to use np.column_stack()
instead of itertools.izip()
.
Also It's not clear that what the getColA
object is and what it's returning. Maybe you can modify that callable object in order to make a more proper result.
Upvotes: 2