7-4列表/元组/字典/集合的内置方法
一.列表
1.1 元素统计(count)
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
l = [1,2,3,4,5,6,7] res = l.count(3) print(res) # 输出结果是1
1.2清除(clear)
l = [1,2,3,4,5,6,7] res = l.clear() print(l)
1.3反向输出列表
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
l = [1,2,3,4,5,6,7] l.reverse() print(l) # [7, 6, 5, 4, 3, 2, 1]
1.4将列表内元素排序(sort)
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
l2 = [1, 342, 45, 23, 4, 435, 34, 324, 234] l2.sort() print(l2) # [1, 4, 23, 34, 45, 234, 324, 342, 435]
ps:默认为升序输出,也可以通过修改默认的排序规则(降序) eg:
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
l2 = [1, 342, 45, 23, 4, 435, 34, 324, 234] l2.sort(reverse=True) print(l2) # [435, 342, 324, 234, 45, 34, 23, 4, 1]
1.5队列(先进先出)
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
l1 = [] l1.append('first') # 先进 l1.append('second') l1.append('third') print(l1.pop(0)) # 先出 print(l1.pop(0)) print(l1.pop(0))
1.6堆栈(先进后出)
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
l1 = [] l1.append('first') l1.append('second') l1.append('third') print(l1.pop()) print(l1.pop()) print(l1.pop()) ''' third second first '''
二.元组(不可变)
2.1元组的作用
作用:能够存储多个元素,元素和元素之间用逗号隔开,元素可以是任何类型,元组不能修改
2.2定义
()内包含若干个元素,元素和元素之间用逗号隔开,元素可以是任何类型,元组不能修改
ps:age = (11,22) 本质age = tuple(11,22)
当定义的元组内只有一个元素时需添加一个逗号,否则定义的将不是元组
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
a=(1) print(type(a)) # <class 'int'> a=(1,) print(type(a)) # <class 'tuple'>
2.3按照索引取值
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
t = (1, 2, 3, 'a', 'b', [1, 2, 3]) print(t[0]) # 1
2.4切片(顾头不顾尾,步长
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
t = (1, 2, 3, 'a', 'b', [1, 2, 3]) print(t[::2]) # (1, 3, 'b')
2.5成员运算(in/not in)
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
t = (1, 2, 3, 'a', 'b', [1, 2, 3]) print('a' in t) # True
2.6元素统计(count)
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
t = (1, 2, 3, 'a', 'b', [1, 2, 3]) print(t.count('a')) # 1
2.7查找元素(index)
t = (1, 2, 3, 'a', 'b', [1, 2, 3]) print(t.index('xxx')) # 报错 当查找元素不再元组中会报错
三.字典
3.1按key取值,可存可取
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
l={ 'a':1, 'b':'ahah', 'c':12 } l['b']='beautiful' l['xxx']='aa' print(l['a']) print(l) # 1 {'a': 1, 'b': 'beautiful', 'c': 12, 'xxx': 'aa'}
ps:在字典中key的值一定要唯一,重复只会输出最后一个相同的key对应的键值对
赋值语句当key不存在的情况下,会自动新增一个键值对,当key存在时会更新为新输入的值
d2 = {'name':'jason','name':'tank','name':'nick','password':123} print(d2) # {'name': 'nick', 'password': 123}
3.2长度(len)
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
l={ 'a':1, 'b':'ahah', 'c':12 } print(len(l)) # 3
3.3成员运算(in/not in 只能判断key的值)
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
d3 = {'name':'jason','password':'123'} print('123' in d3) # False print('name' in d3) # True
3.4删除
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
d3 = {'name':'jason','password':'123'} del d3['name'] print(d3) # {'password': '123'} d3 = {'name':'jason','password':'123'} res = d3.pop('name') # 弹出仅仅是value print(res) print(d3) ''' jason {'password': '123'} ''' d3 = {'name':'jason','password':'123'} res = d3.pop('age') # 当键不存在的时候直接报错 print(d3) d3 = {'name':'jason','password':'123'} d3.clear() print(d3) # {}
3.5keys(),values(),键值对items()
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
d1 = {'name':'jason','password':123} print(d1.keys()) # dict_keys(['name', 'password']) print(d1.values()) # dict_values(['jason', 123]) # 相当于一个列表 print(d1.items()) # dict_items([('name', 'jason'), ('password', 123)]) # 就把它看成是列表套元组,元组的第一个元素是字典的key 第二个元素是字典的value
3.6通过key获取值(get())
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
l={'name':'zhang','hobby':'read'} print(l.get('name','da')) # key存在返回对应的 print(l.get('age','da')) # key不存在 不报错 若不规定第二个参数则默认返回输入的第二个参数 print(l.get('age')) # key不存在 不报错 若不规定第二个参数则默认返回None ''' zhang da None ''' # key不存在 不报错 若不规定第二个参数则默认返回输入的第二个参数
3.7快速建立一个字典(fromkeys())
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
l1 = ['name','password','age','hobby'] print(dict.fromkeys(l1,123)) # {'name': 123, 'password': 123, 'age': 123, 'hobby': 123}
3.8键值对的弹出(popitems())
d1 = {'name':'jason','pwd':123} print(d1.popitem()) # 尾部以元组的形式弹出键值对
3.9setdefault()
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
d1 = {'name':'zhang','pwd':123} res1 = d1.setdefault('name','xxxx') # 当键存在的情况下 不修改值 并且将原先key对应值返回给你 print(d1,res1) res2 = d1.setdefault('age',23) # 当键不存在的情况下 新增一个键值对 并且将新增的键值对的值返回给你 print(d1,res2) ''' {'name': 'zhang', 'pwd': 123} zhang {'name': 'zhang', 'pwd': 123, 'age': 23} 23 '''
3.10update()
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
d1 = {'name':'jason','pwd':123} d2 = {"age":18} d1.update(d2) print(d1) # {'name': 'jason', 'pwd': 123, 'age': 18} d1 = {'name':'jason','pwd':123} d1['age'] = 18 # 新增一个键值对 d1.update(age=666) print(d1) # {'name': 'jason', 'pwd': 123, 'age': 666}
3.11循环
格式:for 变量名 in 字典:
四.集合
4.1集合的作用
用来去重和进行关系运算
4.2集合的定义
s = {1,2,3} # s = set((1,2,3))
当需要定义一个空集合时需要写成s1 = set() 如果写出s1 = {},这会被定义成字典类型
ps:集合的三个原则:
1.每个元素必须时不可变元素
2.没有重复元素
3.无序
4.2长度(len)
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
s = {1,2,3,4} print(len(s)) # 4
4.3成员运算
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
s = {1,2,3,4} print(1 in s) # True
4.4关系运算
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
a={'aa','bb','cc''qq',1,3,4} b={'bb',1,4,'qq'} print(a | b) # a和b的并集 {1, 3, 4, 'aa', 'bb', 'qq', 'ccqq'} print(a - b) # a和b的差集 {'aa', 3, 'ccqq'} print(a ^ b) # a和b的对称差集 {3, 'aa', 'qq', 'ccqq'} print(a & b) # a和b的交集 {1, 'bb', 4}
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
a={1,2,3,4} b={1,2,3} print(a >= b) # a是b的父集 print(b <= a) # b是a的子集
4.5添加元素、
s = {1,2,3,4,5} s.add(666) print(s)
4.6移除元素
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
s = {1,2,3,4,5} print(s.remove(1)) # none print(s) # {2,3,4,5} print(s.remove(6)) # 报错 用remove 命令时移除处对象在集合内没有返回值直接移除,当移除对象不再集合内时会报错 s = {1,2,3,4,5} print(s.discard(888888888)) print(s) # 当使用discard时移除对象不再集合内,不会报错返回值是None
附件: