---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
Cell In[217], line 1
----> 1 ls.remove(321)
ValueError: list.remove(x): x not in list
ls.pop()
'h'
ls
[123, 'test', '2', 'm', 'e', 'c']
ls.pop(10)
---------------------------------------------------------------------------
IndexError Traceback (most recent call last)
Cell In[220], line 1
----> 1 ls.pop(10)
IndexError: pop index out of range
ls.pop(-3)
'm'
ls
[123, 'test', '2', 'e', 'c']
ls[0]=321
ls
[321, 'test', '2', 'e', 'c']
ls[-3:]=[1.1,2.2]
ls
[321, 'test', 1.1, 2.2]
ls.count(1)
0
321in ls
True
1.6 元组 tuple
元素有顺序,可以是任意类型,不可修改
t1 =()
type(t1)
tuple
t1
()
t2 =("qwe")
t2
'qwe'
type(t2)
str
t3 =("12",)
t3
('12',)
type(t3)
tuple
t4=(1,2,True,"ADSAS")
t4
(1, 2, True, 'ADSAS')
t5 =tuple("abd")
t5
('a', 'b', 'd')
t6=tuple([1,2,3,4,5,6])
t6
(1, 2, 3, 4, 5, 6)
# 元素不能修改
t6[3]=0
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
Cell In[248], line 2
1 # 元素不能修改
----> 2 t6[3]=0
TypeError: 'tuple' object does not support item assignment
t1 =tuple("abc")
t1
('a', 'b', 'c')
len(t1)
3
t1.__len__()
3
t1[0]
'a'
t1[-1]
'c'
t1[1::-1]
('b', 'a')
# 省略括号和结构赋值
t1 =(1,4.3)
t2 =1,4.3
t1==t2
True
p1,p2 = t2
p1,p2
(1, 4.3)
p1
1
p2
4.3
p1,p2,p3 = t2
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
Cell In[265], line 1
----> 1 p1,p2,p3 = t2
ValueError: not enough values to unpack (expected 3, got 2)
# 快速交换多个变量的值
A =4
B =5
A,B=B,A
A
5
B
4
ls =(1,2,3,True,1,3,4,2,12)
ls.count(1)
3
ls.count(True)
3
Truein ls
True
Falsein ls
False
# tuple 元素不能修改吗?
t2 =(1,2,3)
t2[1]=3
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
Cell In[279], line 1
----> 1 t2[1]=3
TypeError: 'tuple' object does not support item assignment
---------------------------------------------------------------------------
NameError Traceback (most recent call last)
Cell In[364], line 2
1 # 读取不存在的key会报错
----> 2 d["asd"]
NameError: name 'd' is not defined
---------------------------------------------------------------------------
NameError Traceback (most recent call last)
Cell In[371], line 1
----> 1 d.pop('age1')
NameError: name 'd' is not defined
---------------------------------------------------------------------------
ZeroDivisionError Traceback (most recent call last)
Cell In[13], line 1
----> 1 divide(2,0)
Cell In[11], line 2, in divide(a, b)
1 def divide(a, b):
----> 2 return a / b
ZeroDivisionError: division by zero
"""
异常处理
- 守株待兔的操作
- 如果没有发生异常:不做任何额外处理
- 如果发生了异常,引导程序做出合理化的处理
"""
a =3
b =0try:
result = divide(a,b)print(result)except Exception as e:print(e)else:print("没有发生错误")finally:print("不管是否发生错误,我都会执行")print("这里依然可以执行")
division by zero
不管是否发生错误,我都会执行
这里依然可以执行
# 抛出异常
a =2
b =3defdivide(a, b):ifisinstance(a,int)andisinstance(b,int)and b !=0:return a / b
else:raise Exception("参数错误")
divide(2,1)
2.0
divide(1,0.3)
---------------------------------------------------------------------------
Exception Traceback (most recent call last)
Cell In[23], line 1
----> 1 divide(1,0.3)
Cell In[21], line 10, in divide(a, b)
8 return a / b
9 else:
---> 10 raise Exception("参数错误")
Exception: 参数错误
4. 包和模块、随机数
4.1 包和模块
包和模块是代码组织的一种方式,包就是一个文件夹,模块就是一个源码文件
避免重复造轮子,利用前人写好的包和模块
托管平台:pip 和 conda 管理工具
import numpy as np
np.__version__
'1.23.5'
np.e
2.718281828459045
import os
os.path.exists("./")
True
from matplotlib import pyplot as plt
定义自己的模块
在项目文件夹中新建一个文件夹名为 utils ,在里面新建一个 math.py文件,然后编辑函数
from utils import math
math.add(3,23)
26
math.sub(43,1)
42
4.2 随机数
- 概率论中随机试验产生的结果
- 数据科学中随机数很重要
import random
# 均匀分布# 按照均匀分布生成一个随机数
random.uniform(0,100)
4.646385375119754
random.randint(0,100)
67
# 高斯分布
random.gauss(mu=0,sigma=1)
-2.9510713969400872
# 洗牌操作
x =[1,2,3,4,5,6,7,8,9,0]print(x)
random.shuffle(x)print(x)
# return 返回并跳出函数defget_data():
ls =[0,1,2,3,4,5,6,7,8,9]for ele in ls:return ele
get_data()
0
# yield 构建生成器defget_data():
ls =[0,1,2,3,4,5,6,7,8,9]for ele in ls:yield ele
gen = get_data()
for ele in gen:print(ele)
0
1
2
3
4
5
6
7
8
9
ls =list(range(10))
ls
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
# 列表解析式 : for循环 + if判断
ls1 =[ele for ele in ls if ele %2==1]
ls1
[1, 3, 5, 7, 9]
# 这样也可以构建生成器
gen1 =(ele for ele in ls if ele %2==1)
for ele in gen1:print(ele)
1
3
5
7
9
# 通过生成器来读取数据集defget_dataset():withopen(file="dataset.csv",mode="r",encoding="utf8")as f:
line = f.readline()whileTrue:
line = f.readline()if line:yield line
else:break
🚀返回专栏总目录 文章目录 一、VSS(Virtual Set Size)二、RSS(Resident Set Size)三、PSS(Proportional Set Size)四、USS(Unique Set Size)五、其他工具Linux 提供了多种进程内存占用的度量指标, 它们反映了不同的内存使用特征: VSS 反映进程虚拟内存总需求, 包括未…