Python - 在字典中使用numpy数组作为键的替代方法

时间:2021-01-12 21:22:27

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 repr() function to convert numpy array to a string but it seems very expensive. Is there any better way to achieve same effect?

我对Python numpy很新。我试图在我的一个函数中使用numpy数组作为字典中的键,然后被Python解释器告知numpy数组不可清除。我刚刚发现解决这个问题的一种方法是使用repr()函数将numpy数组转换为字符串,但它似乎非常昂贵。有没有更好的方法来达到同样的效果?

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?

更新:我可以创建一个新类来包含numpy数组,这似乎是实现我想要的正确方法。只是想知道是否有更好的方法?

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

更新2:使用类来包含数组中的数据,然后覆盖__hash__函数是可以接受的,但是,我更喜欢@hpaulj提供的解决方案。将数组/列表转换为符合我需要的元组以更好的方式,因为它不需要额外的类。

2 个解决方案

#1


6  

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.

经过一番研究和阅读所有评论。我想我已经知道了我自己的问题的答案,所以我只是把它们写下来。

  1. Write a class to contain the data in the array and then override __hash__ function to amend the way how it is hashed as mentioned by ZdaR
  2. 编写一个类来包含数组中的数据,然后覆盖__hash__函数来修改ZdaR提到的散列方式

  3. Convert this array to a tuple, which makes the list hashable instantaneously.Thanks to hpaulj
  4. 将此数组转换为元组,这使得列表可以即时清除。感谢hpaulj

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.

我更喜欢方法2,因为它更符合我的需要,也更简单。但是,使用类可能会带来一些额外的好处,因此它也可能有用。

#2


1  

If you want to quickly store a numpy.ndarray as a key in a dictionary, a fast option is to use ndarray.tobytes() which will return a raw python bytes string which is immutable

如果你想快速存储numpy.ndarray作为字典中的键,一个快速选项是使用ndarray.tobytes(),它将返回一个不可变的原始python字节字符串

my_array = numpy.arange(4).reshape((2,2))
my_dict = {}
my_dict[my_array.tobytes()] = None

#1


6  

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.

经过一番研究和阅读所有评论。我想我已经知道了我自己的问题的答案,所以我只是把它们写下来。

  1. Write a class to contain the data in the array and then override __hash__ function to amend the way how it is hashed as mentioned by ZdaR
  2. 编写一个类来包含数组中的数据,然后覆盖__hash__函数来修改ZdaR提到的散列方式

  3. Convert this array to a tuple, which makes the list hashable instantaneously.Thanks to hpaulj
  4. 将此数组转换为元组,这使得列表可以即时清除。感谢hpaulj

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.

我更喜欢方法2,因为它更符合我的需要,也更简单。但是,使用类可能会带来一些额外的好处,因此它也可能有用。

#2


1  

If you want to quickly store a numpy.ndarray as a key in a dictionary, a fast option is to use ndarray.tobytes() which will return a raw python bytes string which is immutable

如果你想快速存储numpy.ndarray作为字典中的键,一个快速选项是使用ndarray.tobytes(),它将返回一个不可变的原始python字节字符串

my_array = numpy.arange(4).reshape((2,2))
my_dict = {}
my_dict[my_array.tobytes()] = None