python进阶
一、列表类型的内置方法
1.1 用途
用来描述多个装备,多个爱好,多种课程等。
1.2 定义
[]内可以有多个任意类型的值,使用逗号进行分割。
name_list = ['panghu','xiaofu','jingxiang','daxiong']
print(F"{name_list[1]}")
xiaofu
1.3 常用操作+内置方法
1.3.1 优先掌握
-
按索引取值(正相+反向),即可存也可取
name_list = ['panghu','xiaofu','jingxiang','daxiong'] print(F"{name_list[-1]}") daxiong
-
切片
name_list = ['panghu','xiaofu','jingxiang','daxiong'] print(F"{name_list[0:3:2]}") ['panghu', 'jingxiang']
-
长度
name_list = ['panghu','xiaofu','jingxiang','daxiong'] print(F"{len(name_list)}") 4
-
成员运算in和not in
name_list = ['panghu','xiaofu','jingxiang','daxiong'] print(F"{'daxiong' in name_list}") print(F"{'pangmei' in name_list}") True False
-
追加值
name_list = ['panghu','xiaofu','jingxiang','daxiong'] name_list.append('xiaodingdang') print(F"{name_list}") ['panghu', 'xiaofu', 'jingxiang', 'daxiong', 'xiaodingdang']
-
删除
name_list = ['panghu','xiaofu','jingxiang','daxiong'] del name_list[3] print(F"{name_list}") ['panghu', 'xiaofu', 'jingxiang']
-
循环
name_list = ['panghu','xiaofu','jingxiang','daxiong'] for name in name_list: print(name) panghu xiaofu jingxiang daxiong
1.3.2 需要掌握
-
insert()
name_list = ['panghu','xiaofu','jingxiang','daxiong'] name_list.insert(3,'xiaodingdang') print(f'{name_list}') ['panghu', 'xiaofu', 'jingxiang', 'xiaodingdang', 'daxiong']
-
pop()(默认删除最后一个元素)
name_list = ['panghu','xiaofu','jingxiang','daxiong'] print(f'{name_list.pop(2)}') print(f'{name_list}') jingxiang ['panghu', 'xiaofu', 'daxiong']
-
remove()
name_list = ['panghu','xiaofu','jingxiang','daxiong'] print(f"{name_list.remove('xiaofu')}") print(f'{name_list}') None ['panghu', 'jingxiang', 'daxiong']
-
count()
name_list = ['panghu','xiaofu','xiaofu','jingxiang','daxiong'] print(f"{name_list.count('xiaofu')}") 2
-
index()(引索)
name_list = ['panghu','xiaofu','jingxiang','daxiong'] print(f"{name_list.index('xiaofu')}") 1
-
clear()
name_list = ['panghu','xiaofu','jingxiang','daxiong'] name_list.clear() print(f"{name_list}") []
-
copy()
name_list = ['panghu','xiaofu','jingxiang','daxiong'] print(f"{name_list.copy()}") ['panghu', 'xiaofu', 'jingxiang', 'daxiong']
-
extend
name_list = ['panghu','xiaofu','jingxiang','daxiong'] name_list2 = ['xiaodingdang'] name_list.extend(name_list2) print(f"{name_list}") ['panghu', 'xiaofu', 'jingxiang', 'daxiong', 'xiaodingdang']
-
reverse()(倒叙)
name_list = ['panghu','xiaofu','jingxiang','daxiong'] name_list.reverse() print(f"{name_list}") ['daxiong', 'jingxiang', 'xiaofu', 'panghu']
-
sort
name_list = ['panghu','xiaofu','jingxiang','daxiong'] name_list.sort() print(f"{name_list}") ['daxiong', 'jingxiang', 'panghu', 'xiaofu']
1.4 存值
存多个值
1.5有序性
有序
1.6 可变性
可变
二、元组类型的内置方法
元组是不可变的列表,即元组的值不可更改,因此元组一般只用于只存不取的需求。也因此元组可以被列表取代掉,所以元组相比较列表使用的很少。元组相比较列表的优点为:列表的值修改后,列表的结构将会发生改变,而元组只需要存储,因此列表在某种程度上而言需要占用更多的内存。但是目前工业上内存已经不是问题了,所以工业上元组一般不会使用。
2.1 用途
多个装备、多个爱好、多门课程。
2.2 定义
在()内可以有多个任意类型的值,逗号分隔元素。
2.3 常用操作+内置方法
-
索引取值
name_tuple = ['panghu','xiaofu','jingxiang','daxiong'] print(f"{name_tuple}") ['panghu', 'xiaofu', 'jingxiang', 'daxiong']
-
切片(顾头不顾尾,步长)
name_tuple = ['panghu','xiaofu','jingxiang','daxiong'] print(f"{name_tuple[1:3:2]}") xiaofu
-
长度
name_tuple = ['panghu','xiaofu','jingxiang','daxiong'] print(f"{len(name_tuple)}") 4
-
成员运算
name_tuple = ['panghu','xiaofu','jingxiang','daxiong'] print(f"{'daxiong' in name_tuple}") True
-
循环
name_tuple = ['panghu','xiaofu','jingxiang','daxiong'] for name in name_tuple: print(name) panghu xiaofu jingxiang daxiong
-
count()
name_tuple = ['panghu','xiaofu','jingxiang','daxiong'] print(f"{name_tuple.count('daxiong')}") 1
-
index()
name_tuple = ['panghu','xiaofu','jingxiang','daxiong'] print(f"{name_tuple.index('daxiong')}") 3
2.4 存值
多个值
2.5 有序性
有序
2.6 可变性
不可变数据类型
三、字典类型的内置方法
3.1 用途
存多个值,但每一个值都有一个key与之对应,key对值有描述功能。多用于存的值表示的是不同的状态时,例如存的值有姓名、年龄、身高、体重、爱好。
3.2 定义
{}内用逗号分隔开多个元素,每一个元素都是key:value的形式,value可以是任意数据类型,而key通常应该是字符串类型,但是key必须为不可变类型。
dic = {'a': 1, 'b': 2} # dic = dict({'a':1,'b':2})
print(f"dic: {dic}")
dic: {'a': 1, 'b': 2}
dic = dict(a=1, b=2, c=3)
print(f"dic: {dic}")
dic: {'a': 1, 'b': 2, 'c': 3}
dic = {1: 'a', 0: 'b'}
print(f"dic[0]: {dic[0]}")
dic[0]: b
3.3 常用操作+内置方法
3.3.1 重点
- 按key存取值:可存可取
dic = {'a': 1, 'b': 2}
print(f"first dic['a']: {dic['a']}")
dic['a'] = 3
print(f"second dic['a']: {dic['a']}")
first dic['a']: 1
second dic['a']: 3
- 长度len
dic = {'a': 1, 'b': 2}
print(f"len(dic): {len(dic)}")
len(dic): 2
- 成员运算in和not in
dic = {'a': 1, 'b': 2}
print(f"'a' in dic: {'a' in dic}")
print(f"1 in dic: {1 in dic}")
'a' in dic: True
1 in dic: False
- 删除
dic = {'a': 1, 'b': 2}
del dic['a']
print(f"dic.get('a'): {dic.get('a')}")
dic.get('a'): None
dic = {'a': 1, 'b': 2}
dic.pop('a') # 指定元素删除
print(f"dic.pop('b'): {dic.pop('b')}")
print(f"dic.get('a'): {dic.get('a')}")
dic.pop('b'): 2
dic.get('a'): None
dic = {'a': 1, 'b': 2}
print(f"dic.popitem(): {dic.popitem()}") # popitem() 方法随机返回并删除字典中的一对键和值(一般删除末尾对)。
dic.popitem(): ('b', 2)
- 键keys()、值values()、键值对items()
dic = {'a': 1, 'b': 2}
print(f"dic.keys(): {dic.keys()}")
print(f"dic.values(): {dic.values()}")
print(f"dic.items(): {dic.items()}")
dic.keys(): dict_keys(['a', 'b'])
dic.values(): dict_values([1, 2])
dic.items(): dict_items([('a', 1), ('b', 2)])
- 循环
dic = {'a': 1, 'b': 2, 'c': 3, 'd': 4}
for k, v in dic.items():
print(k, v)
a 1
b 2
c 3
d 4
3.3.2 需要掌握
- get()
dic = {'a': 1, 'b': 2}
print(f"dic.get('a'): {dic.get('a')}")
print(f"dic.get('c'): {dic.get('c')}")
dic.get('a'): 1
dic.get('c'): None
- update()
dic1 = {'a': 1, 'b': 2}
dic2 = {'c': 3}
dic1.update(dic2)
print(f"dic1: {dic1}")
dic1: {'a': 1, 'b': 2, 'c': 3}
- fromkeys()
dic = dict.fromkeys(['name', 'age', 'sex'], None)
print(f"dic: {dic}")
dic: {'name': None, 'age': None, 'sex': None}
- setdefault()
dic = {'a': 1, 'b': 2}
print(f"dic.setdefault('a'): {dic.setdefault('a',3)}")
print(f"dic: {dic}")
print(f"dic.setdefault('c'): {dic.setdefault('c',3)}")
print(f"dic: {dic}")
dic.setdefault('a'): 1
dic: {'a': 1, 'b': 2}
dic.setdefault('c'): 3
dic: {'a': 1, 'b': 2, 'c': 3}
四、集合类型的内置方法
4.1 用途
用于关系运算的集合体,由于集合内的元素无序且集合元素不可重复,因此集合可以去重,但是去重后的集合会打乱原来元素的顺序。
4.2 定义
{}内用逗号分隔开多个元素,每个元素必须是不可变类型。
s = {1, 2, 1, 'a'}
print(f"s: {s}")
s: {1, 2, 'a'}
s = {1, 2, 1, 'a', 'c'}
for i in s:
print(i)
1
2
c
a
s = set('hello')
print(f"s: {s}")
s: {'e', 'o', 'h', 'l'}
4.3 常用操作和内置方法
4.3.1 重点
1.长度len
s = {1, 2, 'a'}
print(f"len(s): {len(s)}")
len(s): 3
2.成员运算in和not in
s = {1, 2, 'a'}
print(f"1 in s: {1 in s}")
1 in s: True
3.|并集
pythoners = {'jason', 'nick', 'tank', 'sean'}
linuxers = {'nick', 'egon', 'kevin'}
print(f"pythoners|linuxers: {pythoners|linuxers}")
print(f"pythoners.union(linuxers): {pythoners.union(linuxers)}")
pythoners|linuxers: {'egon', 'tank', 'kevin', 'jason', 'nick', 'sean'}
pythoners.union(linuxers): {'egon', 'tank', 'kevin', 'jason', 'nick', 'sean'}
4.&交集
pythoners = {'jason', 'nick', 'tank', 'sean'}
linuxers = {'nick', 'egon', 'kevin'}
print(f"pythoners&linuxers: {pythoners&linuxers}")
print(f"pythoners.intersection(linuxers): {pythoners.intersection(linuxers)}")
pythoners&linuxers: {'nick'}
pythoners.intersection(linuxers): {'nick'}
5.-差集
pythoners = {'jason', 'nick', 'tank', 'sean'}
linuxers = {'nick', 'egon', 'kevin'}
print(f"pythoners-linuxers: {pythoners-linuxers}")
print(f"pythoners.difference(linuxers): {pythoners.difference(linuxers)}")
pythoners-linuxers: {'tank', 'jason', 'sean'}
pythoners.difference(linuxers): {'tank', 'jason', 'sean'}
6.^对称差集
pythoners = {'jason', 'nick', 'tank', 'sean'}
linuxers = {'nick', 'egon', 'kevin'}
print(f"pythoners^linuxers: {pythoners^linuxers}")
print(f"pythoners.symmetric_difference(linuxers): {pythoners.symmetric_difference(linuxers)}")
pythoners^linuxers: {'egon', 'tank', 'kevin', 'jason', 'sean'}
pythoners.symmetric_difference(linuxers): {'egon', 'tank', 'kevin', 'jason', 'sean'}
7.==
pythoners = {'jason', 'nick', 'tank', 'sean'}
linuxers = {'nick', 'egon', 'kevin'}
javers = {'nick', 'egon', 'kevin'}
print(f"pythoners==linuxers: {pythoners==linuxers}")
print(f"javers==linuxers: {javers==linuxers}")
pythoners==linuxers: False
javers==linuxers: True
8.父集:>、>=
pythoners = {'jason', 'nick', 'tank', 'sean'}
linuxers = {'nick', 'egon', 'kevin'}
javaers = {'jason', 'nick'}
print(f"pythoners>linuxers: {pythoners>linuxers}")
print(f"pythoners>=linuxers: {pythoners>=linuxers}")
print(f"pythoners>=javaers: {pythoners>=javaers}")
print(f"pythoners.issuperset(javaers): {pythoners.issuperset(javaers)}")
pythoners>linuxers: False
pythoners>=linuxers: False
pythoners>=javaers: True
pythoners.issuperset(javaers): True
9.子集:<、<=
pythoners = {'jason', 'nick', 'tank', 'sean'}
linuxers = {'nick', 'egon', 'kevin'}
javaers = {'jason', 'nick'}
print(f"pythoners<linuxers: {pythoners<linuxers}")
print(f"pythoners<=linuxers: {pythoners<=linuxers}")
print(f"javaers.issubset(javaers): {javaers.issubset(javaers)}")
pythoners<linuxers: False
pythoners<=linuxers: False
javaers.issubset(javaers): True
4.3.2 掌握
1.add()
s = {1, 2, 'a'}
s.add(3)
print(s)
{1, 2, 3, 'a'}
2.remove()
s = {1, 2, 'a'}
s.remove(1)
print(s)
{2, 'a'}
3.difference_update()
pythoners = {'jason', 'nick', 'tank', 'sean'}
linuxers = {'nick', 'egon', 'kevin'}
pythoners.difference_update(linuxers)
print(f"pythoners.difference_update(linuxers): {pythoners}")
pythoners.difference_update(linuxers): {'tank', 'jason', 'sean'}
4.discard()
s = {1, 2, 'a'}
s.discard(3)
print(s)
{1, 2, 'a'}
5.isdisjoint()
# set之isdisjoint(),集合没有共同的部分返回True,否则返回False
pythoners = {'jason', 'nick', 'tank', 'sean'}
linuxers = {'nick', 'egon', 'kevin'}
pythoners.isdisjoint(linuxers)
print(f"pythoners.isdisjoint(linuxers): {pythoners.isdisjoint(linuxers)}")
pythoners.isdisjoint(linuxers): False