解构
In [8]: lst = [1,2]
In [9]: lst
Out[9]: [1, 2]
In [10]: first,second = lst #解构
In [11]: print(first,second)
1 2
按照元素顺序,把线性结构的元素赋值给变量
解构的变化
In [21]: lst = list(range(10))
In [22]: lst
Out[22]: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
In [23]: head,*mid,tail = lst
In [24]: head
Out[24]: 0
In [25]: tail
Out[25]: 9
In [26]: mid
Out[26]: [1, 2, 3, 4, 5, 6, 7, 8]
In [27]: head,*_,tail = lst
In [28]: head
Out[28]: 0
In [29]: tail
Out[29]: 9
Python的一个惯例, 使用单个下划线 _ 表示丢弃该变量
封装
In [12]: t = 1,2
In [13]: t
Out[13]: (1, 2)
In [14]: type(t)
Out[14]: tuple
In [15]: t1 = (1,2)
In [16]: t2 = 1,2
In [17]: t1
Out[17]: (1, 2)
In [18]: t2
Out[18]: (1, 2)
封装出来的一定是元祖
集合--没有重复元素
定义及初始化
In [31]: s = set()
In [32]: s
Out[32]: set()
In [33]: s = {1,2,3}
In [34]: s
Out[34]: {1, 2, 3}
In [35]: s = set(range(5))
In [36]: s
Out[36]: {0, 1, 2, 3, 4}
集合的操作
增加
In [37]: s
Out[37]: {0, 1, 2, 3, 4}
In [38]: s.add(9) #增加单个元素
In [39]: s
Out[39]: {0, 1, 2, 3, 4, 9}
In [40]: s.add(9) #增加已经存在的元素,什么也不做
In [41]: s
Out[41]: {0, 1, 2, 3, 4, 9}
In [42]: s.update(range(10,15)) #增加多个元素
In [43]: s
Out[43]: {0, 1, 2, 3, 4, 9, 10, 11, 12, 13, 14}
删除
In [44]: s
Out[44]: {0, 1, 2, 3, 4, 9, 10, 11, 12, 13, 14}
In [45]: s.remove(1)
In [46]: s
Out[46]: {0, 2, 3, 4, 9, 10, 11, 12, 13, 14}
In [47]: s.pop()
Out[47]: 0
In [48]: s
Out[48]: {2, 3, 4, 9, 10, 11, 12, 13, 14}
In [49]: s.pop()
Out[49]: 2
In [50]: s.pop()
Out[50]: 3
In [51]: s.pop()
Out[51]: 4
In [52]: s.pop()
Out[52]: 9
In [53]: s.pop()
Out[53]: 10
In [54]: s.pop()
Out[54]: 11
In [55]: s.discard(13)
In [56]: s
Out[56]: {12, 14}
remove 删除给定的元素, 元素不存在抛出KeyError
discard 删除给定的元素, 元素不存在,什么也不做
pop 随机删除一个元素并返回, 集合为空,抛出KeyError
clear 清空集合
集合不能修改单个元素,不能通过索引查找,集合没有访问单个元素的方法,集合元素没有顺序,元素必须课hash
集合运算
交集、并集、差集、对分差集
In [57]: s1 = {1,2,3}
In [58]: s2 = {2,3,4}
In [59]: s1.intersection(s2)
Out[59]: {2, 3}
In [60]: s1 & s2
Out[60]: {2, 3}
In [61]:
In [61]: s1.union(s2)
Out[61]: {1, 2, 3, 4}
In [62]: s1 | s2
Out[62]: {1, 2, 3, 4}
In [63]:
In [63]: s1.difference(s2)
Out[63]: {1}
In [64]: s1 - s2
Out[64]: {1}
In [65]:
In [65]: s1.symmetric_difference(s2)
Out[65]: {1, 4}
In [66]: s1 ^ s2
Out[66]: {1, 4}
集合的判断
In [67]: s1 = {1,2,3,4}
In [68]: s2 = {2,3}
In [69]: s2.issubset(s1)
Out[69]: True
In [70]: s1.issuperset(s2)
Out[70]: True
In [71]:
In [71]:
In [71]: s1.isdisjoint(s2) # 判断两个集合是否有交集, 如果有交集返回False, 没有交集返回True
Out[71]: False
字典--是一种key-value结构
定义及初始化
In [72]: d = {}
In [73]: d
Out[73]: {}
In [74]: d = dict{}
In [75]: d = dict()
In [76]: d
Out[76]: {}
In [77]: d = {'a':1,'b':2}
In [78]: d
Out[78]: {'a': 1, 'b': 2}
In [79]: d = dict([('a', 1), ('b', 2)])
In [80]: d
Out[80]: {'a': 1, 'b': 2}
In [81]: d = dict.fromkeys(range(5),'ab')
In [82]: d
Out[82]: {0: 'ab', 1: 'ab', 2: 'ab', 3: 'ab', 4: 'ab'}
增加、修改
In [83]: d
Out[83]: {0: 'ab', 1: 'ab', 2: 'ab', 3: 'ab', 4: 'ab'}
In [84]:
In [84]: d['a'] = 5
In [85]: d
Out[85]: {0: 'ab', 1: 'ab', 2: 'ab', 3: 'ab', 4: 'ab', 'a': 5}
In [86]: d.update([('b',5),('c',6)])
In [87]: d
Out[87]: {0: 'ab', 1: 'ab', 2: 'ab', 3: 'ab', 4: 'ab', 'a': 5, 'b': 5, 'c': 6}
In [88]: d.update({'d':2,'r':6})
In [89]: d
Out[89]:
{0: 'ab',
1: 'ab',
2: 'ab',
3: 'ab',
4: 'ab',
'a': 5,
'd': 2,
'b': 5,
'c': 6,
'r': 6}
删除
In [91]: d.pop(0)
Out[91]: 'ab'
In [92]: d
Out[92]: {1: 'ab', 2: 'ab', 3: 'ab', 4: 'ab', 'a': 5, 'd': 2, 'b': 5, 'c': 6, 'r': 6}
In [93]: d.popitem() #随机删除
Out[93]: (1, 'ab')
字典的遍历
In [100]: d
Out[100]: {'b': 5, 'c': 6, 'r': 6}
In [101]: for x in d:
...: print(x)
...:
b
c
r
In [102]: d.keys()
Out[102]: dict_keys(['b', 'c', 'r'])
In [103]: d.values()
Out[103]: dict_values([5, 6, 6])
In [104]: d.items()
Out[104]: dict_items([('b', 5), ('c', 6), ('r', 6)])
In [105]: for v in d.values():
...: print(v)
...:
5
6
6
In [106]: for x in d.items():
...: print(x)
...:
('b', 5)
('c', 6)
('r', 6)
In [107]: for k,v in d.items():
...: print(k,v)
...:
b 5
c 6
r 6
字典的key不可重复,需要可hash