[python数据结构] hashable, list, tuple, set, frozenset

学习 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).

"""
python list concatenate:

>>> [[0, 0]] + [‘fill X‘]
[[0, 0], ‘fill X‘]
>>> [[0, 0]] + [‘fill X‘, (4, 0)]
[[0, 0], ‘fill X‘, (4, 0)]

"""

  

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).

set, fronzeset

p.p1 { margin: 0.0px 0.0px 0.0px 0.0px; font: 23.0px "Lucida Grande"; color: #222222; background-color: #ffffff }
span.s1 { }
span.s2 { color: #0072aa }

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

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

>>> a_set

{(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.

# problem when initialize a set
>>> {(0, 0)}
{(0, 0)}
# this only return one `0`
>>> set((0, 0))
{0}

  

  

p.p1 { margin: 0.0px 0.0px 0.0px 0.0px; font: 23.0px "Lucida Grande"; color: #222222; background-color: #ffffff }
span.s1 { }
span.s2 { font: 22.2px Courier; color: #0072aa }
span.s3 { color: #0072aa }

时间: 2024-12-25 13:16:13

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

Python数据结构

1. 元组(tuple) 元组由不同的元素组成,每个元素可以储存不同类型的数据,如字符串.数字甚至元组.元组是写保护的,即元组创建后不能再做任何修改操作. 1.1 元组的创建 Tuple(元组)由一系列元素组成,所有元素被包含在一对圆括号中.创建元组时可以不指定元素个数,但一旦创建后就不能修改长度 元组的创建格式如下:tuple_name = (元素1,元素2,-) 如果创建空元组,只需要一对空的圆括号:tuple_name = () 如果创建的元组只包含一个元素,应在元素后面加上逗号,以区分元

[笔记]python数据结构之线性表:linkedlist链表,stack栈,queue队列

python数据结构之线性表 python内置了很多高级数据结构,list,dict,tuple,string,set等,在使用的时候十分舒心.但是,如果从一个初学者的角度利用python学习数据结构时,这些高级的数据结构可能给我们以迷惑. 比如,使用list实现queue的时候,入队操作append()时间复杂度可以认为是O(1),但是,出队操作pop(0)的时间复杂度就是O(n). 如果是想利用python学学数据结构的话,我觉得还是自己实现一遍基本的数据结构为好. 1.链表 在这里,我想使

python数据结构之 列表和元组

python数据结构之 列表和元组 序列:序列是一种数据结构,它包含的元素都进行了编号(从0开始).典型的序列包括列表.字符串和元组.其中,列表是可变的(可以进行修改),而元组和字符串是不可变的(一旦创建了就是固定的).序列中包含6种内建的序列,包括列表.元组.字符串.Unicode字符串.buffer对象.xrange对象. 列表的声明: mylist = [] 2.列表的操作: (1) 序列的分片: 用法:mylist[startIndex:endIndex:step] exam: myli

Python数据结构:列表、元组和字典

在Python中有三种内建的数据结构——列表list.元组tuple和字典dict 列表中的项目包括在方括号中,项目之间用逗号分割 元组和列表十分类似,只不过元组和字符串一样是不可变的 即你不能修改元组.元组通过圆括号中用逗号分割的项目定义. 元组最通常的用法是用在打印语句中 age = 22 name = 'Swaroop' print '%s is %d years old' % (name, age) print 'Why is %s playing with that python?'

Python学习教程(Python学习路线+Python学习视频):Python数据结构

Python学习教程(Python学习路线+Python学习视频):Python数据结构   数据结构引言:   数据结构是组织数据的方式,以便能够更好的存储和获取数据.数据结构定义数据之间的关系和对这些数据的操作方式.数据结构屏蔽了数据存储和操作的细节,让程序员能更好的处理业务逻辑,同时拥有快速的数据存储和获取方式. 在这篇文章中,你将了解到多种数据结构以及这些数据结构在Python中实现的方式.    抽象数据类型和数据结构 数据结构是抽象数据类型(ADT)的实现,通常,是通过编程语言提供的

python 数据结构考题

1. 以下关于python数据结构说法正确的是 python中list可以动态的更新, 但是不容许嵌套 python中tuple可以动态更新, 但是不容许嵌套 python中dict保存键值对, 并且键值对是有序的 python中list的元素可以是tuple 解析:A list允许更新,允许嵌套 B tuple不允许更新    C 键值对没有顺序的概念 2. 关于Python中的复数,下列说法错误的是() 表是复数的语法是real + image j 实部和虚部都是浮点数 虚部必须后缀j,且必

python 数据结构类型总结

文章目录 字符串: 1.用引号来创建字符串,单双引号都可(三引号也可:三引号可以复制复杂的字符串,三引号内一个字符串可占多行,字符串中可包含换行符.制表符和其他特殊字符).eg. >>>var1 = 'Hello!'nr >>>var2 = "Hi!" 2.不支持单字符类型,单字符作为一个字符串使用.3.访问字符串的值,用方括号来截取字符串,通过索引获取字符,索引从0开始.eg. >>>var1 = 'Hello!' >>

4. Python里面如何实现tuple和list的转换

# Python里面如何实现tuple和list的转换 li = [1,2,3,4] print(tuple(li)) li1 = (1,2,3,4) print(list(li1)) str = '123' print(list(str))

Python数据结构——散列表

散列表的实现常常叫做散列(hashing).散列仅支持INSERT,SEARCH和DELETE操作,都是在常数平均时间执行的.需要元素间任何排序信息的操作将不会得到有效的支持. 散列表是普通数组概念的推广.如果空间允许,可以提供一个数组,为每个可能的关键字保留一个位置,就可以运用直接寻址技术. 当实际存储的关键字比可能的关键字总数较小时,采用散列表就比较直接寻址更为有效.在散列表中,不是直接把关键字用作数组下标,而是根据关键字计算出下标,这种 关键字与下标之间的映射就叫做散列函数. 1.散列函数