标签:top 容器 value sch defaults rar 传输 rest 完全
Python 的元组与列表类似,不同之处在于元组的元素不能修改。
元组使用小括号,列表使用方括号。
元组创建很简单,只需要在括号中添加元素,并使用逗号隔开即可。
不可变的tuple有什么意义?因为tuple不可变,所以代码更安全。如果可能,能用tuple代替list就尽量用tuple。
定义:与列表类似,只不过[]改成()
特性:
1.可存放多个值
2.不可变
3.按照从左到右的顺序定义元组元素,下标从0开始顺序访问,有序
lass tuple(object): """ tuple() -> empty tuple tuple(iterable) -> tuple initialized from iterable‘s items If the argument is a tuple, the return value is the same object. """ def count(self, value): # real signature unknown; restored from __doc__ """ T.count(value) -> integer -- return number of occurrences of value """ return 0 def index(self, value, start=None, stop=None): # real signature unknown; restored from __doc__ """ T.index(value, [start, [stop]]) -> integer -- return first index of value. Raises ValueError if the value is not present. """ return 0 def __add__(self, y): # real signature unknown; restored from __doc__ """ x.__add__(y) <==> x+y """ pass def __contains__(self, y): # real signature unknown; restored from __doc__ """ x.__contains__(y) <==> y in x """ pass def __eq__(self, y): # real signature unknown; restored from __doc__ """ x.__eq__(y) <==> x==y """ pass def __getattribute__(self, name): # real signature unknown; restored from __doc__ """ x.__getattribute__(‘name‘) <==> x.name """ pass def __getitem__(self, y): # real signature unknown; restored from __doc__ """ x.__getitem__(y) <==> x[y] """ pass def __getnewargs__(self, *args, **kwargs): # real signature unknown pass def __getslice__(self, i, j): # real signature unknown; restored from __doc__ """ x.__getslice__(i, j) <==> x[i:j] Use of negative indices is not supported. """ pass def __ge__(self, y): # real signature unknown; restored from __doc__ """ x.__ge__(y) <==> x>=y """ pass def __gt__(self, y): # real signature unknown; restored from __doc__ """ x.__gt__(y) <==> x>y """ pass def __hash__(self): # real signature unknown; restored from __doc__ """ x.__hash__() <==> hash(x) """ pass def __init__(self, seq=()): # known special case of tuple.__init__ """ tuple() -> empty tuple tuple(iterable) -> tuple initialized from iterable‘s items If the argument is a tuple, the return value is the same object. # (copied from class doc) """ pass def __iter__(self): # real signature unknown; restored from __doc__ """ x.__iter__() <==> iter(x) """ pass def __len__(self): # real signature unknown; restored from __doc__ """ x.__len__() <==> len(x) """ pass def __le__(self, y): # real signature unknown; restored from __doc__ """ x.__le__(y) <==> x<=y """ pass def __lt__(self, y): # real signature unknown; restored from __doc__ """ x.__lt__(y) <==> x<y """ pass def __mul__(self, n): # real signature unknown; restored from __doc__ """ x.__mul__(n) <==> x*n """ pass @staticmethod # known case of __new__ def __new__(S, *more): # real signature unknown; restored from __doc__ """ T.__new__(S, ...) -> a new object with type S, a subtype of T """ pass def __ne__(self, y): # real signature unknown; restored from __doc__ """ x.__ne__(y) <==> x!=y """ pass def __repr__(self): # real signature unknown; restored from __doc__ """ x.__repr__() <==> repr(x) """ pass def __rmul__(self, n): # real signature unknown; restored from __doc__ """ x.__rmul__(n) <==> n*x """ pass def __sizeof__(self): # real signature unknown; restored from __doc__ """ T.__sizeof__() -- size of T in memory, in bytes """ pass tuple
如下实例:
tup1 = (‘Google‘, ‘Runoob‘, 1997, 2000); tup2 = (1, 2, 3, 4, 5 ); tup3 = "a", "b", "c", "d";
创建空元组
tup1 = ();
元组中只包含一个元素时,需要在元素后面添加逗号
tup1 = (50,);
元组与字符串类似,下标索引从0开始,可以进行截取,组合等。
元组可以使用下标索引来访问元组中的值,如下实例:
#!/usr/bin/python3 tup1 = (‘Google‘, ‘Runoob‘, 1997, 2000) tup2 = (1, 2, 3, 4, 5, 6, 7 ) print ("tup1[0]: ", tup1[0]) print ("tup2[1:5]: ", tup2[1:5])
以上实例输出结果:
tup1[0]: Google tup2[1:5]: (2, 3, 4, 5)
元组中的元素值是不允许修改的,但我们可以对元组进行连接组合,如下实例:
#!/usr/bin/python3 tup1 = (12, 34.56); tup2 = (‘abc‘, ‘xyz‘) # 以下修改元组元素操作是非法的。 # tup1[0] = 100 # 创建一个新的元组 tup3 = tup1 + tup2; print (tup3)
以上实例输出结果:
(12, 34.56, ‘abc‘, ‘xyz‘)
元组中的元素值是不允许删除的,但我们可以使用del语句来删除整个元组,如下实例:
#!/usr/bin/python3 tup = (‘Google‘, ‘Runoob‘, 1997, 2000) print (tup) del tup; print ("删除后的元组 tup : ") print (tup)
以上实例元组被删除后,输出变量会有异常信息,输出如下所示:
删除后的元组 tup : Traceback (most recent call last): File "test.py", line 8, in <module> print (tup) NameError: name ‘tup‘ is not defined
与字符串一样,元组之间可以使用 + 号和 * 号进行运算。这就意味着他们可以组合和复制,运算后会生成一个新的元组。
Python 表达式 | 结果 | 描述 |
---|---|---|
len((1, 2, 3)) | 3 | 计算元素个数 |
(1, 2, 3) + (4, 5, 6) | (1, 2, 3, 4, 5, 6) | 连接 |
[‘Hi!‘] * 4 | [‘Hi!‘, ‘Hi!‘, ‘Hi!‘, ‘Hi!‘] | 复制 |
3 in (1, 2, 3) | True | 元素是否存在 |
for x in (1, 2, 3): print x, | 1 2 3 | 迭代 |
因为元组也是一个序列,所以我们可以访问元组中的指定位置的元素,也可以截取索引中的一段元素,如下所示:
元组:
L = (‘Google‘, ‘Taobao‘, ‘Runoob‘)
Python 表达式
结果 | 描述 | |
---|---|---|
L[2] | ‘Runoob!‘ | 读取第三个元素 |
L[-2] | ‘Taobao‘ | 反向读取;读取倒数第二个元素 |
L[1:] | (‘Taobao‘, ‘Runoob!‘) | 截取元素,从第二个开始后的所有元素。 |
运行实例如下:
>>> L = (‘Google‘, ‘Taobao‘, ‘Runoob‘) >>> L[2] ‘Runoob‘ >>> L[-2] ‘Taobao‘ >>> L[1:] (‘Taobao‘, ‘Runoob‘)
Python元组包含了以下内置函数
序号 | 方法及描述 | 实例 |
---|---|---|
1 | len(tuple) 计算元组元素个数。 |
>>> tuple1 = (‘Google‘, ‘Runoob‘, ‘Taobao‘)
>>> len(tuple1)
3
>>>
|
2 | max(tuple) 返回元组中元素最大值。 |
>>> tuple2 = (‘5‘, ‘4‘, ‘8‘)
>>> max(tuple2)
‘8‘
>>>
|
3 | min(tuple) 返回元组中元素最小值。 |
>>> tuple2 = (‘5‘, ‘4‘, ‘8‘)
>>> min(tuple2)
‘4‘
>>>
|
4 | tuple(seq) 将列表转换为元组。 |
>>> list1= [‘Google‘, ‘Taobao‘, ‘Runoob‘, ‘Baidu‘]
>>> tuple1=tuple(list1)
>>> tuple1
(‘Google‘, ‘Taobao‘, ‘Runoob‘, ‘Baidu‘)
|
Python在显示只有1个元素的tuple时,也会加一个逗号,
,以免你误解成数学计算意义上的括号。
最后来看一个“可变的”tuple:
>>> t = (‘a‘, ‘b‘, [‘A‘, ‘B‘]) >>> t[2][0] = ‘X‘ >>> t[2][1] = ‘Y‘ >>> t (‘a‘, ‘b‘, [‘X‘, ‘Y‘])
这个tuple定义的时候有3个元素,分别是‘a‘
,‘b‘
和一个list。不是说tuple一旦定义后就不可变了吗?怎么后来又变了?
别急,我们先看看定义的时候tuple包含的3个元素:
当我们把list的元素‘A‘
和‘B‘
修改为‘X‘
和‘Y‘
后,tuple变为:
表面上看,tuple的元素确实变了,但其实变的不是tuple的元素,而是list的元素。tuple一开始指向的list并没有改成别的list,所以,tuple所谓的“不变”是说,tuple的每个元素,指向永远不变。即指向‘a‘
,就不能改成指向‘b‘
,指向一个list,就不能改成指向其他对象,但指向的这个list本身是可变的!
理解了“指向不变”后,要创建一个内容也不变的tuple怎么做?那就必须保证tuple的每一个元素本身也不能变。
字典是另一种可变容器模型,且可存储任意类型对象。
Python内置了字典:dict的支持,dict全称dictionary,在其他语言中也称为map,使用键-值(key-value)存储,具有极快的查找速度。
常用操作:
class dict(object): """ dict() -> new empty dictionary dict(mapping) -> new dictionary initialized from a mapping object‘s (key, value) pairs dict(iterable) -> new dictionary initialized as if via: d = {} for k, v in iterable: d[k] = v dict(**kwargs) -> new dictionary initialized with the name=value pairs in the keyword argument list. For example: dict(one=1, two=2) """ def clear(self): # real signature unknown; restored from __doc__ """ 清除内容 """ """ D.clear() -> None. Remove all items from D. """ pass def copy(self): # real signature unknown; restored from __doc__ """ 浅拷贝 """ """ D.copy() -> a shallow copy of D """ pass @staticmethod # known case def fromkeys(S, v=None): # real signature unknown; restored from __doc__ """ dict.fromkeys(S[,v]) -> New dict with keys from S and values equal to v. v defaults to None. """ pass def get(self, k, d=None): # real signature unknown; restored from __doc__ """ 根据key获取值,d是默认值 """ """ D.get(k[,d]) -> D[k] if k in D, else d. d defaults to None. """ pass def has_key(self, k): # real signature unknown; restored from __doc__ """ 是否有key """ """ D.has_key(k) -> True if D has a key k, else False """ return False def items(self): # real signature unknown; restored from __doc__ """ 所有项的列表形式 """ """ D.items() -> list of D‘s (key, value) pairs, as 2-tuples """ return [] def iteritems(self): # real signature unknown; restored from __doc__ """ 项可迭代 """ """ D.iteritems() -> an iterator over the (key, value) items of D """ pass def iterkeys(self): # real signature unknown; restored from __doc__ """ key可迭代 """ """ D.iterkeys() -> an iterator over the keys of D """ pass def itervalues(self): # real signature unknown; restored from __doc__ """ value可迭代 """ """ D.itervalues() -> an iterator over the values of D """ pass def keys(self): # real signature unknown; restored from __doc__ """ 所有的key列表 """ """ D.keys() -> list of D‘s keys """ return [] def pop(self, k, d=None): # real signature unknown; restored from __doc__ """ 获取并在字典中移除 """ """ D.pop(k[,d]) -> v, remove specified key and return the corresponding value. If key is not found, d is returned if given, otherwise KeyError is raised """ pass def popitem(self): # real signature unknown; restored from __doc__ """ 获取并在字典中移除 """ """ D.popitem() -> (k, v), remove and return some (key, value) pair as a 2-tuple; but raise KeyError if D is empty. """ pass def setdefault(self, k, d=None): # real signature unknown; restored from __doc__ """ 如果key不存在,则创建,如果存在,则返回已存在的值且不修改 """ """ D.setdefault(k[,d]) -> D.get(k,d), also set D[k]=d if k not in D """ pass def update(self, E=None, **F): # known special case of dict.update """ 更新 {‘name‘:‘alex‘, ‘age‘: 18000} [(‘name‘,‘sbsbsb‘),] """ """ D.update([E, ]**F) -> None. Update D from dict/iterable E and F. If E present and has a .keys() method, does: for k in E: D[k] = E[k] If E present and lacks .keys() method, does: for (k, v) in E: D[k] = v In either case, this is followed by: for k in F: D[k] = F[k] """ pass def values(self): # real signature unknown; restored from __doc__ """ 所有的值 """ """ D.values() -> list of D‘s values """ return [] def viewitems(self): # real signature unknown; restored from __doc__ """ 所有项,只是将内容保存至view对象中 """ """ D.viewitems() -> a set-like object providing a view on D‘s items """ pass def viewkeys(self): # real signature unknown; restored from __doc__ """ D.viewkeys() -> a set-like object providing a view on D‘s keys """ pass def viewvalues(self): # real signature unknown; restored from __doc__ """ D.viewvalues() -> an object providing a view on D‘s values """ pass def __cmp__(self, y): # real signature unknown; restored from __doc__ """ x.__cmp__(y) <==> cmp(x,y) """ pass def __contains__(self, k): # real signature unknown; restored from __doc__ """ D.__contains__(k) -> True if D has a key k, else False """ return False def __delitem__(self, y): # real signature unknown; restored from __doc__ """ x.__delitem__(y) <==> del x[y] """ pass def __eq__(self, y): # real signature unknown; restored from __doc__ """ x.__eq__(y) <==> x==y """ pass def __getattribute__(self, name): # real signature unknown; restored from __doc__ """ x.__getattribute__(‘name‘) <==> x.name """ pass def __getitem__(self, y): # real signature unknown; restored from __doc__ """ x.__getitem__(y) <==> x[y] """ pass def __ge__(self, y): # real signature unknown; restored from __doc__ """ x.__ge__(y) <==> x>=y """ pass def __gt__(self, y): # real signature unknown; restored from __doc__ """ x.__gt__(y) <==> x>y """ pass def __init__(self, seq=None, **kwargs): # known special case of dict.__init__ """ dict() -> new empty dictionary dict(mapping) -> new dictionary initialized from a mapping object‘s (key, value) pairs dict(iterable) -> new dictionary initialized as if via: d = {} for k, v in iterable: d[k] = v dict(**kwargs) -> new dictionary initialized with the name=value pairs in the keyword argument list. For example: dict(one=1, two=2) # (copied from class doc) """ pass def __iter__(self): # real signature unknown; restored from __doc__ """ x.__iter__() <==> iter(x) """ pass def __len__(self): # real signature unknown; restored from __doc__ """ x.__len__() <==> len(x) """ pass def __le__(self, y): # real signature unknown; restored from __doc__ """ x.__le__(y) <==> x<=y """ pass def __lt__(self, y): # real signature unknown; restored from __doc__ """ x.__lt__(y) <==> x<y """ pass @staticmethod # known case of __new__ def __new__(S, *more): # real signature unknown; restored from __doc__ """ T.__new__(S, ...) -> a new object with type S, a subtype of T """ pass def __ne__(self, y): # real signature unknown; restored from __doc__ """ x.__ne__(y) <==> x!=y """ pass def __repr__(self): # real signature unknown; restored from __doc__ """ x.__repr__() <==> repr(x) """ pass def __setitem__(self, i, y): # real signature unknown; restored from __doc__ """ x.__setitem__(i, y) <==> x[i]=y """ pass def __sizeof__(self): # real signature unknown; restored from __doc__ """ D.__sizeof__() -> size of D in memory, in bytes """ pass __hash__ = None dict
定义:{key1:value1,key2:value2},key-value结构,key必须可hash
特性:
1.可存放多个值
2.可修改指定key对应的值,可变
3.无序
如果用dict实现,只需要一个“名字”-“成绩”的对照表,直接根据名字查找成绩,无论这个表有多大,查找速度都不会变慢。用Python写一个dict如下:
>>> d = {‘Michael‘: 95, ‘Bob‘: 75, ‘Tracy‘: 85} >>> d[‘Michael‘] 95
为什么dict查找速度这么快?因为dict的实现原理和查字典是一样的。假设字典包含了1万个汉字,我们要查某一个字,一个办法是把字典从第一页往后翻,直到找到我们想要的字为止,这种方法就是在list中查找元素的方法,list越大,查找越慢。
第二种方法是先在字典的索引表里(比如部首表)查这个字对应的页码,然后直接翻到该页,找到这个字。无论找哪个字,这种查找速度都非常快,不会随着字典大小的增加而变慢。
dict就是第二种实现方式,给定一个名字,比如‘Michael‘
,dict在内部就可以直接计算出Michael
对应的存放成绩的“页码”,也就是95
这个数字存放的内存地址,直接取出来,所以速度非常快。
你可以猜到,这种key-value存储方式,在放进去的时候,必须根据key算出value的存放位置,这样,取的时候才能根据key直接拿到value。
由于一个key只能对应一个value,所以,多次对一个key放入value,后面的值会把前面的值冲掉:
>>> d[‘Jack‘] = 90 >>> d[‘Jack‘] 90 >>> d[‘Jack‘] = 88 >>> d[‘Jack‘] 88
如果key不存在,dict就会报错:
>>> d[‘Thomas‘] Traceback (most recent call last): File "<stdin>", line 1, in <module> KeyError: ‘Thomas‘
要避免key不存在的错误,有两种办法,一是通过in
判断key是否存在:
>>> ‘Thomas‘ in d False
二是通过dict提供的get方法,如果key不存在,可以返回None,或者自己指定的value:
>>> d.get(‘Thomas‘) >>> d.get(‘Thomas‘, -1) -1
注意:返回None
的时候Python的交互式命令行不显示结果。
要删除一个key,用pop(key)
方法,对应的value也会从dict中删除:
>>> d.pop(‘Bob‘) 75 >>> d {‘Michael‘: 95, ‘Tracy‘: 85}
请务必注意,dict内部存放的顺序和key放入的顺序是没有关系的。
和list比较,dict有以下几个特点:
而list相反:
所以,dict是用空间来换取时间的一种方法。
dict可以用在需要高速查找的很多地方,在Python代码中几乎无处不在,正确使用dict非常重要,需要牢记的第一条就是dict的key必须是不可变对象。
这是因为dict根据key来计算value的存储位置,如果每次计算相同的key得出的结果不同,那dict内部就完全混乱了。这个通过key计算位置的算法称为哈希算法(Hash)。
要保证hash的正确性,作为key的对象就不能变。在Python中,字符串、整数等都是不可变的,因此,可以放心地作为key。而list是可变的,就不能作为key:
>>> key = [1, 2, 3] >>> d[key] = ‘a list‘ Traceback (most recent call last): File "<stdin>", line 1, in <module> TypeError: unhashable type: ‘list‘
字典的每个键值(key=>value)对用冒号(:)分割,每个对之间用逗号(,)分割,整个字典包括在花括号({})中 ,格式如下所示:
d = {key1 : value1, key2 : value2 }
键必须是唯一的,但值则不必。
值可以取任何数据类型,但键必须是不可变的,如字符串,数字或元组。
一个简单的字典实例:
dict = {‘Alice‘: ‘2341‘, ‘Beth‘: ‘9102‘, ‘Cecil‘: ‘3258‘}
也可如此创建字典:
dict1 = { ‘abc‘: 456 }; dict2 = { ‘abc‘: 123, 98.6: 37 };
把相应的键放入熟悉的方括弧,如下实例:
#!/usr/bin/python3 dict = {‘Name‘: ‘Runoob‘, ‘Age‘: 7, ‘Class‘: ‘First‘} print ("dict[‘Name‘]: ", dict[‘Name‘]) print ("dict[‘Age‘]: ", dict[‘Age‘])
以上实例输出结果:
dict[‘Name‘]: Runoob dict[‘Age‘]: 7
如果用字典里没有的键访问数据,会输出错误如下:
#!/usr/bin/python3 dict = {‘Name‘: ‘Runoob‘, ‘Age‘: 7, ‘Class‘: ‘First‘}; print ("dict[‘Alice‘]: ", dict[‘Alice‘])
以上实例输出结果:
Traceback (most recent call last): File "test.py", line 5, in <module> print ("dict[‘Alice‘]: ", dict[‘Alice‘]) KeyError: ‘Alice‘
向字典添加新内容的方法是增加新的键/值对,修改或删除已有键/值对如下实例:
#!/usr/bin/python3 dict = {‘Name‘: ‘Runoob‘, ‘Age‘: 7, ‘Class‘: ‘First‘} dict[‘Age‘] = 8; # 更新 Age dict[‘School‘] = "python教程" # 添加信息 print ("dict[‘Age‘]: ", dict[‘Age‘]) print ("dict[‘School‘]: ", dict[‘School‘])
以上实例输出结果:
dict[‘Age‘]: 8 dict[‘School‘]: python教程
能删单一的元素也能清空字典,清空只需一项操作。
显示删除一个字典用del命令,如下实例:
#!/usr/bin/python3 dict = {‘Name‘: ‘Runoob‘, ‘Age‘: 7, ‘Class‘: ‘First‘} del dict[‘Name‘] # 删除键 ‘Name‘ dict.clear() # 删除字典 del dict # 删除字典 print ("dict[‘Age‘]: ", dict[‘Age‘]) print ("dict[‘School‘]: ", dict[‘School‘])
但这会引发一个异常,因为用执行 del 操作后字典不再存在:
Traceback (most recent call last): File "test.py", line 9, in <module> print ("dict[‘Age‘]: ", dict[‘Age‘]) TypeError: ‘type‘ object is not subscriptable
注:del() 方法后面也会讨论。
字典值可以没有限制地取任何python对象,既可以是标准的对象,也可以是用户定义的,但键不行。
两个重要的点需要记住:
1)不允许同一个键出现两次。创建时如果同一个键被赋值两次,后一个值会被记住,如下实例:
#!/usr/bin/python3 dict = {‘Name‘: ‘Runoob‘, ‘Age‘: 7, ‘Name‘: ‘小菜鸟‘} print ("dict[‘Name‘]: ", dict[‘Name‘])
以上实例输出结果:
dict[‘Name‘]: 小菜鸟
2)键必须不可变,所以可以用数字,字符串或元组充当,而用列表就不行,如下实例:
#!/usr/bin/python3 dict = {[‘Name‘]: ‘Runoob‘, ‘Age‘: 7} print ("dict[‘Name‘]: ", dict[‘Name‘])
以上实例输出结果:
Traceback (most recent call last): File "test.py", line 3, in <module> dict = {[‘Name‘]: ‘Runoob‘, ‘Age‘: 7} TypeError: unhashable type: ‘list‘
Python字典包含了以下内置函数:
序号 | 函数及描述 | 实例 |
---|---|---|
1 | len(dict) 计算字典元素个数,即键的总数。 |
>>> dict = {‘Name‘: ‘Runoob‘, ‘Age‘: 7, ‘Class‘: ‘First‘}
>>> len(dict)
3
|
2 | str(dict) 输出字典以可打印的字符串表示。 |
>>> dict = {‘Name‘: ‘Runoob‘, ‘Age‘: 7, ‘Class‘: ‘First‘}
>>> str(dict)
"{‘Name‘: ‘Runoob‘, ‘Class‘: ‘First‘, ‘Age‘: 7}"
|
3 | type(variable) 返回输入的变量类型,如果变量是字典就返回字典类型。 |
>>> dict = {‘Name‘: ‘Runoob‘, ‘Age‘: 7, ‘Class‘: ‘First‘}
>>> type(dict)
<class ‘dict‘>
|
Python字典包含了以下内置方法:
序号 | 函数及描述 |
---|---|
1 | radiansdict.clear() 删除字典内所有元素 |
2 | radiansdict.copy() 返回一个字典的浅复制 |
3 | radiansdict.fromkeys() 创建一个新字典,以序列seq中元素做字典的键,val为字典所有键对应的初始值 |
4 | radiansdict.get(key, default=None) 返回指定键的值,如果值不在字典中返回default值 |
5 | key in dict 如果键在字典dict里返回true,否则返回false |
6 | radiansdict.items() 以列表返回可遍历的(键, 值) 元组数组 |
7 | radiansdict.keys() 以列表返回一个字典所有的键 |
8 | radiansdict.setdefault(key, default=None) 和get()类似, 但如果键不存在于字典中,将会添加键并将值设为default |
9 | radiansdict.update(dict2) 把字典dict2的键/值对更新到dict里 |
10 | radiansdict.values() 以列表返回字典中的所有值 |
li = [11,22,33,44] for item in li: print(item)
li = [11,22,33] for k,v in enumerate(li, 1): print(k,v)
print range(1, 10) # 结果:[1, 2, 3, 4, 5, 6, 7, 8, 9] print(range(1, 10, 2)) # 结果:[1, 3, 5, 7, 9] print(range(30, 0, -2)) # 结果:[30, 28, 26, 24, 22, 20, 18, 16, 14, 12, 10, 8, 6, 4, 2]
集合(set)是一个无序不重复元素的序列。
基本功能是进行成员关系测试和删除重复元素。
可以使用大括号({})或者 set()函数创建集合,注意:创建一个空集合必须用 set() 而不是 { },因为 { } 是用来创建一个空字典。
定义:由不同元素组成的集合,集合中是一组无序排列的可hash值,可以作为字典的key
特性:
1.集合的目的是将不同的值存放到一起,不同的集合间用来做关系运算,无需纠结于集合中单个值
in
not in
==
!=
<,<=
>,>=
|,|=:合集
&.&=:交集
-,-=:差集
^,^=:对称差分
class set(object): """ set() -> new empty set object set(iterable) -> new set object Build an unordered collection of unique elements. """ def add(self, *args, **kwargs): # real signature unknown """ Add an element to a set. This has no effect if the element is already present. """ pass def clear(self, *args, **kwargs): # real signature unknown """ Remove all elements from this set. """ pass def copy(self, *args, **kwargs): # real signature unknown """ Return a shallow copy of a set. """ pass def difference(self, *args, **kwargs): # real signature unknown """ 相当于s1-s2 Return the difference of two or more sets as a new set. (i.e. all elements that are in this set but not the others.) """ pass def difference_update(self, *args, **kwargs): # real signature unknown """ Remove all elements of another set from this set. """ pass def discard(self, *args, **kwargs): # real signature unknown """ 与remove功能相同,删除元素不存在时不会抛出异常 Remove an element from a set if it is a member. If the element is not a member, do nothing. """ pass def intersection(self, *args, **kwargs): # real signature unknown """ 相当于s1&s2 Return the intersection of two sets as a new set. (i.e. all elements that are in both sets.) """ pass def intersection_update(self, *args, **kwargs): # real signature unknown """ Update a set with the intersection of itself and another. """ pass def isdisjoint(self, *args, **kwargs): # real signature unknown """ Return True if two sets have a null intersection. """ pass def issubset(self, *args, **kwargs): # real signature unknown """ 相当于s1<=s2 Report whether another set contains this set. """ pass def issuperset(self, *args, **kwargs): # real signature unknown """ 相当于s1>=s2 Report whether this set contains another set. """ pass def pop(self, *args, **kwargs): # real signature unknown """ Remove and return an arbitrary set element. Raises KeyError if the set is empty. """ pass def remove(self, *args, **kwargs): # real signature unknown """ Remove an element from a set; it must be a member. If the element is not a member, raise a KeyError. """ pass def symmetric_difference(self, *args, **kwargs): # real signature unknown """ 相当于s1^s2 Return the symmetric difference of two sets as a new set. (i.e. all elements that are in exactly one of the sets.) """ pass def symmetric_difference_update(self, *args, **kwargs): # real signature unknown """ Update a set with the symmetric difference of itself and another. """ pass def union(self, *args, **kwargs): # real signature unknown """ 相当于s1|s2 Return the union of sets as a new set. (i.e. all elements that are in either set.) """ pass def update(self, *args, **kwargs): # real signature unknown """ Update a set with the union of itself and others. """ pass def __and__(self, *args, **kwargs): # real signature unknown """ Return self&value. """ pass def __contains__(self, y): # real signature unknown; restored from __doc__ """ x.__contains__(y) <==> y in x. """ pass def __eq__(self, *args, **kwargs): # real signature unknown """ Return self==value. """ pass def __getattribute__(self, *args, **kwargs): # real signature unknown """ Return getattr(self, name). """ pass def __ge__(self, *args, **kwargs): # real signature unknown """ Return self>=value. """ pass def __gt__(self, *args, **kwargs): # real signature unknown """ Return self>value. """ pass def __iand__(self, *args, **kwargs): # real signature unknown """ Return self&=value. """ pass def __init__(self, seq=()): # known special case of set.__init__ """ set() -> new empty set object set(iterable) -> new set object Build an unordered collection of unique elements. # (copied from class doc) """ pass def __ior__(self, *args, **kwargs): # real signature unknown """ Return self|=value. """ pass def __isub__(self, *args, **kwargs): # real signature unknown """ Return self-=value. """ pass def __iter__(self, *args, **kwargs): # real signature unknown """ Implement iter(self). """ pass def __ixor__(self, *args, **kwargs): # real signature unknown """ Return self^=value. """ pass def __len__(self, *args, **kwargs): # real signature unknown """ Return len(self). """ pass def __le__(self, *args, **kwargs): # real signature unknown """ Return self<=value. """ pass def __lt__(self, *args, **kwargs): # real signature unknown """ Return self<value. """ pass @staticmethod # known case of __new__ def __new__(*args, **kwargs): # real signature unknown """ Create and return a new object. See help(type) for accurate signature. """ pass def __ne__(self, *args, **kwargs): # real signature unknown """ Return self!=value. """ pass def __or__(self, *args, **kwargs): # real signature unknown """ Return self|value. """ pass def __rand__(self, *args, **kwargs): # real signature unknown """ Return value&self. """ pass def __reduce__(self, *args, **kwargs): # real signature unknown """ Return state information for pickling. """ pass def __repr__(self, *args, **kwargs): # real signature unknown """ Return repr(self). """ pass def __ror__(self, *args, **kwargs): # real signature unknown """ Return value|self. """ pass def __rsub__(self, *args, **kwargs): # real signature unknown """ Return value-self. """ pass def __rxor__(self, *args, **kwargs): # real signature unknown """ Return value^self. """ pass def __sizeof__(self): # real signature unknown; restored from __doc__ """ S.__sizeof__() -> size of S in memory, in bytes """ pass def __sub__(self, *args, **kwargs): # real signature unknown """ Return self-value. """ pass def __xor__(self, *args, **kwargs): # real signature unknown """ Return self^value. """ pass __hash__ = None 查看 查看
定义:存8bit整数,数据基于网络传输或内存变量存储到硬盘时需要转成bytes类型,字符串前置b代表为bytes类型
>>> x ‘hello sb‘ >>> x.encode(‘gb2312‘) b‘hello sb‘
#!/usr/bin/python3 student = {‘Tom‘, ‘Jim‘, ‘Mary‘, ‘Tom‘, ‘Jack‘, ‘Rose‘} print(student) # 输出集合,重复的元素被自动去掉 # 成员测试 if(‘Rose‘ in student) : print(‘Rose 在集合中‘) else : print(‘Rose 不在集合中‘) # set可以进行集合运算 a = set(‘abracadabra‘) b = set(‘alacazam‘) print(a) print(a - b) # a和b的差集 print(a | b) # a和b的并集 print(a & b) # a和b的交集 print(a ^ b) # a和b中不同时存在的元素
以上实例输出结果:
{‘Jack‘, ‘Rose‘, ‘Mary‘, ‘Jim‘, ‘Tom‘} Rose 在集合中 {‘r‘, ‘b‘, ‘a‘, ‘c‘, ‘d‘} {‘r‘, ‘b‘, ‘d‘} {‘a‘, ‘l‘, ‘z‘, ‘b‘, ‘m‘, ‘d‘, ‘r‘, ‘c‘} {‘a‘, ‘c‘} {‘l‘, ‘z‘, ‘b‘, ‘m‘, ‘d‘, ‘r‘}
set和dict类似,也是一组key的集合,但不存储value。由于key不能重复,所以,在set中,没有重复的key。
要创建一个set,需要提供一个list作为输入集合:
>>> s = set([1, 2, 3]) >>> s {1, 2, 3}
注意,传入的参数[1, 2, 3]
是一个list,而显示的{1, 2, 3}
只是告诉你这个set内部有1,2,3这3个元素,显示的顺序也不表示set是有序的。。
重复元素在set中自动被过滤:
>>> s = set([1, 1, 2, 2, 3, 3]) >>> s {1, 2, 3}
通过add(key)
方法可以添加元素到set中,可以重复添加,但不会有效果:
>>> s.add(4) >>> s {1, 2, 3, 4} >>> s.add(4) >>> s {1, 2, 3, 4}
通过remove(key)
方法可以删除元素:
>>> s.remove(4) >>> s {1, 2, 3}
set可以看成数学意义上的无序和无重复元素的集合,因此,两个set可以做数学意义上的交集、并集等操作:
>>> s1 = set([1, 2, 3]) >>> s2 = set([2, 3, 4]) >>> s1 & s2 {2, 3} >>> s1 | s2 {1, 2, 3, 4}
set和dict的唯一区别仅在于没有存储对应的value,但是,set的原理和dict一样,所以,同样不可以放入可变对象,因为无法判断两个可变对象是否相等,也就无法保证set内部“不会有重复元素”。试试把list放入set,看看是否会报错。
上面我们讲了,str是不变对象,而list是可变对象。
对于可变对象,比如list,对list进行操作,list内部的内容是会变化的,比如:
>>> a = [‘c‘, ‘b‘, ‘a‘] >>> a.sort() >>> a [‘a‘, ‘b‘, ‘c‘]
而对于不可变对象,比如str,对str进行操作呢:
>>> a = ‘abc‘ >>> a.replace(‘a‘, ‘A‘) ‘Abc‘ >>> a ‘abc‘
虽然字符串有个replace()
方法,也确实变出了‘Abc‘
,但变量a
最后仍是‘abc‘
,应该怎么理解呢?
我们先把代码改成下面这样:
>>> a = ‘abc‘ >>> b = a.replace(‘a‘, ‘A‘) >>> b ‘Abc‘ >>> a ‘abc‘
要始终牢记的是,a
是变量,而‘abc‘
才是字符串对象!有些时候,我们经常说,对象a
的内容是‘abc‘
,但其实是指,a
本身是一个变量,它指向的对象的内容才是‘abc‘
:
当我们调用a.replace(‘a‘, ‘A‘)
时,实际上调用方法replace
是作用在字符串对象‘abc‘
上的,而这个方法虽然名字叫replace
,但却没有改变字符串‘abc‘
的内容。相反,replace
方法创建了一个新字符串‘Abc‘
并返回,如果我们用变量b
指向该新字符串,就容易理解了,变量a
仍指向原有的字符串‘abc‘
,但变量b
却指向新字符串‘Abc‘
了:
所以,对于不变对象来说,调用对象自身的任意方法,也不会改变该对象自身的内容。相反,这些方法会创建新的对象并返回,这样,就保证了不可变对象本身永远是不可变的。
使用key-value存储结构的dict在Python中非常有用,选择不可变对象作为key很重要,最常用的key是字符串。
tuple虽然是不变对象,但试试把(1, 2, 3)
和(1, [2, 3])
放入dict或set中,并解释结果。
按存值个数区分
标量/原子类型 | 数字,字符串 |
容器类型 | 列表,元组,字典 |
按可变不可变区分
可变 | 列表,字典 |
不可变 | 数字,字符串,元组 |
按访问顺序区分
直接访问 | 数字 |
顺序访问(序列类型) | 字符串,列表,元组 |
key值访问(映射类型) | 字典 |
一、数字和字符串
对于 数字 和 字符串 而言,赋值、浅拷贝和深拷贝无意义,因为其永远指向同一个内存地址。
import copy # ######### 数字、字符串 ######### n1 = 123 # n1 = "i am alex age 10" print(id(n1)) # ## 赋值 ## n2 = n1 print(id(n2)) # ## 浅拷贝 ## n2 = copy.copy(n1) print(id(n2)) # ## 深拷贝 ## n3 = copy.deepcopy(n1) print(id(n3))
其他基本数据类型
对于字典、元祖、列表 而言,进行赋值、浅拷贝和深拷贝时,其内存地址的变化是不同的。
1、赋值
赋值,只是创建一个变量,该变量指向原来内存地址,如:
n1 = {"k1": "wu", "k2": 123, "k3": ["alex", 456]} n2 = n1
2、浅拷贝
浅拷贝,在内存中只额外创建第一层数据
import copy n1 = {"k1": "wu", "k2": 123, "k3": ["alex", 456]} n3 = copy.copy(n1)
3、深拷贝
深拷贝,在内存中将所有的数据重新创建一份(排除最后一层,即:python内部对字符串和数字的优化)
import copy n1 = {"k1": "wu", "k2": 123, "k3": ["alex", 456]} n4 = copy.deepcopy(n1)
标签:top 容器 value sch defaults rar 传输 rest 完全
原文地址:http://www.cnblogs.com/zhangyux/p/5991426.html