f. c.
f. c.

Reputation: 1135

How to add an empty item in a dictionary in python?

I have created a dictionary in python like this:

 dictionary = dict(zip(numpy.arange(5), numpy.arange(5)*10))

And I use the dictionary as follows:

 y = np.vectorize(lambda x: dictionary[x])

Now I want to add a special situation: if the input x is an empty array, output also an empty array. And I tried to add an empty array in the dictionary:

 dictionary[np.array([], dtype='int64')] = np.array([], dtype='int64')

But I got an error:

*** TypeError: unhashable type: 'numpy.ndarray'

Why does it not work? How should I handle the empty array here?

Upvotes: 0

Views: 878

Answers (1)

hpaulj
hpaulj

Reputation: 231665

Look at your dicitonary:

In [21]: dd = dict(zip(numpy.arange(5), numpy.arange(5)*10))
In [22]: dd
Out[22]: {0: 0, 1: 10, 2: 20, 3: 30, 4: 40}

the keys are numbers. The same thing can be produced without numpy:

In [23]: dict(zip(range(5), range(0,50,10)))
Out[23]: {0: 0, 1: 10, 2: 20, 3: 30, 4: 40}

Your vectorize array access:

In [29]: y = np.vectorize(lambda x: dd[x], otypes=[int])
In [30]: y([0,1,3])
Out[30]: array([ 0, 10, 30])
In [31]: y([])
Out[31]: array([], dtype=int64)

I added the otypes so that the [] works.

I don't see why you want to add an entry that has an array key. None of the other keys are arrays. And as you found out an array can't be a key.

dictionary[np.array([], dtype='int64')]

vectorize passes scalar values from the argument to your function. It does not pass an array. So there's no point to having an array, empty or not, as a key.

Whether np.vectorize is the best tool for using this dictionary is another question. Usually it doesn't improve speed over iterative access. Using a dictionary might the underlying problem, since it can only be accessed on key at a time.

===

Without the otypes, vectorize raises an error

ValueError: cannot call `vectorize` on size 0 inputs unless `otypes` is set

vectorize makes a trial call to the function to determine the return dtype.

===

Here's a more robust version of your y, one that won't choke on a missing key:

In [32]: y = np.vectorize(lambda x: dd.get(x,-100), otypes=[int])
In [33]: y([1,2,3,10])
Out[33]: array([  10,   20,   30, -100])

Upvotes: 1

Related Questions