哈哈,不变 [英] Hashable, immutable

查看:162
本文介绍了哈哈,不变的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

从最近的一个问题(见创建一个python中的字典,由列表索引)我意识到我可能对python中可变的和不可变对象的含义有错误的概念。

From a recent SO question (see Create a dictionary in python which is indexed by lists) I realized I probably had a wrong conception of the meaning of hashable and immutable objects in python.


  • 在实践中,hashable是什么意思?

  • hashable和immmutable之间的关系是什么?

  • 是否存在可变的对象,不可变的不可变的对象?

推荐答案

散列是以一种可重复的方式将一些大量数据转换成更小的数据(通常是单个整数)的过程,以便可以在常量表中查找-time( O(1)),这对于高性能算法和数据结构很重要。

Hashing is the process of converting some large amount of data into a much smaller amount (typically a single integer) in a repeatable way so that it can be looked up in a table in constant-time (O(1)), which is important for high-performance algorithms and data structures.

一个href =http://en.wikipedia.org/wiki/Immutable_object =noreferrer>不变性是一个想法,一个对象在创建之后不会以某种重要的方式改变,特别是在任何可能改变该对象的哈希值的方法。

Immutability is the idea that an object will not change in some important way after it has been created, especially in any way that might change the hash value of that object.

这两个想法是相关的,因为用作哈希键的对象通常必须是不可变的,因此它们的哈希值不会t改变。如果允许更改,那么该数据结构(如散列表)中该对象的位置会发生变化,那么哈希效率的整体目的将被消除。

The two ideas are related because objects which are used as hash keys must typically be immutable so their hash value doesn't change. If it was allowed to change then the location of that object in a data structure such as a hashtable would change and then the whole purpose of hashing for efficiency is defeated.

真正了解您应该尝试以C / C ++语言实现自己的散列表,或者阅读 HashMap 类的Java实现。

To really grasp the idea you should try to implement your own hashtable in a language like C/C++, or read the Java implementation of the HashMap class.

这篇关于哈哈,不变的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆