学习 cs212 unit4 时遇到了 tuple, list, set 同时使用的问题,并且进行了拼接、合并操作。于是我就被弄混了。所以在这里进行一下总结。

hashable and unhashable

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.

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.

--from Hashable, immutable

There are three concepts to grasp when trying to understand idhash and the == and isoperators: identityvalue and hash value. Not all objects have all three.

  1. All objects have an identity, though even this can be a little slippery in some cases. The id function returns a number corresponding to an object's identity (in cpython, it returns the memory address of the object, but other interpreters may return something else). If two objects (that exist at the same time) have the same identity, they're actually two references to the same object. The is operator compares items by identity, a is b is equivalent to id(a) == id(b).

    Identity can get a little confusing when you deal with objects that are cached somewhere in their implementation. For instance, the objects for small integers and strings in cpython are not remade each time they're used. Instead, existing objects are returned any time they're needed. You should not rely on this in your code though, because it's an implementation detail of cpython (other interpreters may do it differently or not at all).

  2. All objects also have a value, though this is a bit more complicated. Some objects do not have a meaningful value other than their identity (so value an identity may be synonymous, in some cases). Value can be defined as what the == operator compares, so any time a == b, you can say that a and b have the same value. Container objects (like lists) have a value that is defined by their contents, while some other kinds of objects will have values based on their attributes. Objects of different types can sometimes have the same values, as with numbers: 0 == 0.0 == 0j == decimal.Decimal("0") == fractions.Fraction(0) == False (yep, bools are numbers in Python, for historic reasons).

    If a class doesn't define an __eq__ method (to implement the == operator), it will inherit the default version from object and its instances will be compared solely by their identities. This is appropriate when otherwise identical instances may have important semantic differences. For instance, two different sockets connected to the same port of the same host need to be treated differently if one is fetching an HTML webpage and the other is getting an image linked from that page, so they don't have the same value.

  3. In addition to a value, some objects have a hash value, which means they can be used as dictionary keys (and stored in sets). The function hash(a) returns the object a's hash value, a number based on the object's value. The hash of an object must remain the same for the lifetime of the object, so it only makes sense for an object to be hashable if its value is immutable (either because it's based on the object's identity, or because it's based on contents of the object that are themselves immutable).

    Multiple different objects may have the same hash value, though well designed hash functions will avoid this as much as possible. Storing objects with the same hash in a dictionary is much less efficient than storing objects with distinct hashes (each hash collision requires more work). Objects are hashable by default (since their default value is their identity, which is immutable). If you write an __eq__ method in a custom class, Python will disable this default hash implementation, since your __eq__ function will define a new meaning of value for its instances. You'll need to write a __hash__ method as well, if you want your class to still be hashable. If you inherit from a hashable class but don't want to be hashable yourself, you can set __hash__ = None in the class body.

    --from Difference between hash() and id()

list

Lists are mutable sequences, typically used to store collections of homogeneous(同种的) items (where the precise degree of similarity will vary by application).

  1. """
  2. python list concatenate:
  3.  
  4. >>> [[0, 0]] + ['fill X']
  5. [[0, 0], 'fill X']
  6. >>> [[0, 0]] + ['fill X', (4, 0)]
  7. [[0, 0], 'fill X', (4, 0)]
  8.  
  9. """
  1. # use `list` with iterable
  2. list( (1, 2, 3) )
  3. [1, 2, 3]
  4. # user `list comprehension` with tuple or list
  5. [ (1, 2, 3) ]
  6. [(1, 2, 3)]
  7. [ [1, 2, 3] ]
  8. [[1, 2, 3]]

这种语法应该死记硬背吧

tuple

Tuples are immutable sequences, typically used to store collections of heterogeneous(异种的,不同成分的) data (such as the 2-tuples produced by the enumerate() built-in). Tuples are also used for cases where an immutable sequence of homogeneous data is needed (such as allowing storage in a set or dict instance).

tuple 是不可以修改的,所以

  1. """
  2. a_tuple = (0,)
  3. a_tuple
  4. (0,)
  5.  
  6. a_tuple[0] = 1
  7. Traceback (most recent call last):
  8. File "<input>", line 1, in <module>
  9. TypeError: 'tuple' object does not support item assignment
  10.  
  11. # but you use `+` can concatenate tuples
  12. a_tuple + (1, 1, 1)
  13. (0, 1, 1, 1)
  14. # because concatenate tuples return a new tuple, the original tuple is same
  15. a_tuple
  16. (0,)
  17. """

  

  

  

set, fronzeset

A set object is an unordered collection of distinct hashable objects.(This means that set is mutable, but it's member must be immutable, so

  1. # set memmber must be immutable
    >>> a_set.add([1,2])
  2. Traceback (most recent call last):
  3. File "<stdin>", line 1, in <module>
  4. TypeError: unhashable type: 'list'
    # but set is mutable, so it can add member
    >>> a_set.add((4, 9))
  5.  
  6. >>> a_set
  7.  
  8. {(0, 0), (4, 9)}

Common uses include membership testing, removing duplicates from a sequence, and computing mathematical operations such as intersection, union, difference, and symmetric difference.

The frozenset type is immutable and hashable — its contents cannot be altered after it is created; it can therefore be used as a dictionary key or as an element of another set.

But i get in trobule when intialize a set.

  1. # problem when initialize a set
  2. >>> {(0, 0)}
  3. {(0, 0)}
  4. # this only return one `0`
  5. >>> set((0, 0))
  6. {0}

  

  

[python数据结构] hashable, list, tuple, set, frozenset的更多相关文章

  1. Python数据结构之二——tuple(元组)

    Python版本:3.6.2  操作系统:Windows  作者:SmallWZQ 列表和元组是Python中最常见的内建序列.元组与列表一样,但是tuple一旦创建就不能修改.创建元组的语法非常简单 ...

  2. Python数据结构之四——set(集合)

    Python版本:3.6.2  操作系统:Windows  作者:SmallWZQ 经过几天的回顾和学习,我终于把Python 3.x中的基础知识介绍好啦.下面将要继续什么呢?让我想想先~~~嗯,还是 ...

  3. python数据结构与算法之list

    1. 数据结构的操作 作为一种包含元素的数据结构,需要提供一些“标准”操作: 创建和销毁 判断是否空,如果容量有限,还需判断是否满 向结构中加入元素或从中删除 访问结构里的元素 不同的编程语言可能影响 ...

  4. Python 数据结构理解分享

    摘要:分享学习Python数据结构的一些理解,主要包含序列(如列表和元组),映射(如字典)以及集合3中基本的数据结构,以及可变和不可变数据类型. Python 中的数据结构是根据某种方式将数据元素组合 ...

  5. Python中的List,Tuple,Dic,Set

    Python中的List,Tuple,Dic,Set List定义 序列是Python中最基本的数据结构.序列中的每个元素都分配一个数字 - 它的位置,或索引,第一个索引是0,第二个索引是1,依此类推 ...

  6. Python数据结构与循环语句

    # Python数据结构与循环语句:   首先编程是一项技能,类似跑步,期初不必在意细节,能使用起来就行,等学的游刃有余了再回过头来关注细节问题也不迟.  关于买书: 学会python之后,才需要买书 ...

  7. Python数据结构和类型--解压序列赋值多个变量

    Python数据结构和类型 1.1 解压序列赋值给多个变量 现在有一个包含N个元素的元组或者是序列,怎样将它里面的值解压后同时赋值给N个变量? 解决思路:先通过简单的解压赋值给多个变量,前提是变量的数 ...

  8. python 数据结构考题

    1. 以下关于python数据结构说法正确的是 python中list可以动态的更新, 但是不容许嵌套 python中tuple可以动态更新, 但是不容许嵌套 python中dict保存键值对, 并且 ...

  9. python数据结构与算法

    最近忙着准备各种笔试的东西,主要看什么数据结构啊,算法啦,balahbalah啊,以前一直就没看过这些,就挑了本简单的<啊哈算法>入门,不过里面的数据结构和算法都是用C语言写的,而自己对p ...

随机推荐

  1. Java 内部类种类及使用解析【转】

    内部类Inner Class 将相关的类组织在一起,从而降低了命名空间的混乱. 一个内部类可以定义在另一个类里,可以定义在函数里,甚至可以作为一个表达式的一部分. Java中的内部类共分为四种: 静态 ...

  2. Nginx主动连接与被动连接的差别

    1.主动连接是指Nginx主动发起的同上游server的连接:被动连接是指Nginx接收到的来自client主动发起的连接; 2.主动连接用ngx_peer_connection_t结构体表示:被动连 ...

  3. Flume、Kafka、Storm结合

    Todo: 对Flume的sink进行重构,调用kafka的消费生产者(producer)发送消息; 在Sotrm的spout中继承IRichSpout接口,调用kafka的消息消费者(Consume ...

  4. MySQL学习总结(四)数据的基本操作以及MySQL运算符和常用函数

    数据库是存储数据库对象的仓库,数据库的基本对象是表,表用来存储数据.关于数据的操作也就是我们常说的CRUD,C指的是CREATE(插入数据记录).R指的是READ(查询数据记录).U指的是UPDATE ...

  5. oracle 11g 中 (oracle 10g) crsctl 的 替换命令

     oracle 11g 中 (oracle 10g) crsctl 的 替换命令 Deprecated Command Replacement Commands crs_stat  ---集群状态 ...

  6. Centos中配置环境变量

    以Java的开发环境Jdk为例. 将jdk-9.0.1放置在/usr/local下(UNIX规范),然后我们将jdk配置到环境变量中去. $ mv jdk- /usr/local $ vim /etc ...

  7. CentOS设置程序开机自启动的方法

    转自:http://www.centos.bz/2011/09/centos-setup-process-startup-boot/ 在CentOS系统下,主要有两种方法设置自己安装的程序开机启动. ...

  8. VS2017 - Winform 简单托盘小程序

    界面比较简单,主要两个button 一个NotifyIcon 和 右键菜单 控件, NotifyIcon 属性,如下: 并为NotifyIcon指定了DoubleClick事件: 主窗体增加两个事件: ...

  9. activiti自己定义流程之整合(三):整合自己定义表单创建模型

    本来在创建了表单之后应该是表单列表和预览功能.可是我看了看整合的代码,和之前没实用angularjs的基本没有什么变化,一些极小的变动也仅仅是基于angularjs的语法,因此全然能够參考之前说些的表 ...

  10. HTTP协议之http状态码详解

    什么是HTTP状态码 HTTP状态码的作用是:Web服务器用来告诉客户端,发生了什么事. 状态码位于HTTP Response 的第一行中,会返回一个”三位数字的状态码“和一个“状态消息”. ”三位数 ...