Python中的可哈希对象

Posted by Annie's Blog on May 15, 2016

字典(dictionary)的key集合(set)的元素 都必须是可哈希的(hashable)

不可哈希(unhashable)的数据类型:

  • list: 可用tule代替

  • set: 可用frozenset 代替

  • dictionary: 暂时没有较好的替代者, 不过可以自己写一个可哈希的字典, 如下:

class hashabledict(dict): 
	def __hash__(self): 
		return hash(tuple(sorted(self.items())))

参考:http://stackoverflow.com/questions/1306631/python-add-list-to-set