Python的基本使用(numpy、pandas、matplotlib)

numpy、pandas、matplotlib

1. numpy

numpy(Numerical Python 的简称)是 Python 语言的一个扩展程序库,支持大量的维度数组与矩阵运算,此外也针对数组运算提供大量的数学函数库。它的主要特点是:

N维数组对象:用于存储单一数据类型的多维数组。

快速的元素级运算:如加法、减法、乘法等。

广播:一种强大的机制,使得不同大小的数组之间可以进行数学运算。

线性代数、统计和傅里叶变换等:提供了大量的高级数学函数。

常用代码:

numpy(Numerical Python 的简称)是 Python 中的一个基础库,用于处理大型多维数组和矩阵,以及执行各种与这些数组相关的数学操作。以下是一些 numpy 的常用代码示例:

1. 导入 numpy

python复制代码

import numpy as np

2. 创建数组

python复制代码

# 一维数组

arr1d = np.array([1, 2, 3, 4, 5])

# 二维数组(矩阵)

arr2d = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]])

# 使用 zeros, ones, empty 创建特定形状的数组

zeros_arr = np.zeros((3, 3))

ones_arr = np.ones((2, 4))

empty_arr = np.empty((2, 3)) # 注意:内容未初始化,可能是任何值

# 使用 arange, linspace 创建一维数组

arange_arr = np.arange(0, 10, 2) # 从 0 开始,到 10(不包括),步长为 2

linspace_arr = np.linspace(0, 1, 5) # 从 0 到 1,生成 5 个等间隔的数

# 使用 random 创建随机数组

random_arr = np.random.rand(3, 3) # 生成 0 到 1 之间的随机数

randint_arr = np.random.randint(0, 10, (3, 3)) # 生成 0 到 9 之间的随机整数

3. 数组操作

python复制代码

# 数组运算(元素级)

result = arr1d + arr1d # 对应元素相加

result = arr2d * 2 # 所有元素乘以 2

# 索引和切片

element = arr2d[0, 0] # 获取第一个元素

row = arr2d[1, :] # 获取第二行

col = arr2d[:, 1] # 获取第二列

# 形状(shape)和大小(size)

shape = arr2d.shape # 获取形状,例如 (3, 3)

size = arr2d.size # 获取元素总数

# 数据类型(dtype)

dtype = arr1d.dtype # 获取数据类型,例如 dtype('int64')

# 排序

sorted_arr = np.sort(arr1d)

# 条件选择

mask = arr1d > 3 

selected_elements = arr1d[mask]

# 数组重塑(reshape)

reshaped_arr = arr1d.reshape((1, 5))

# 连接数组(concatenate)

concat_arr = np.concatenate((arr1d, [6, 7]))

# 数组转置(transpose)

transposed_arr = arr2d.T

# 矩阵乘法

dot_product = np.dot(arr2d, arr2d.T)

4. 统计和聚合

python复制代码

# 最小值、最大值、平均值、中位数等

min_val = np.min(arr1d)

max_val = np.max(arr1d)

mean_val = np.mean(arr1d)

median_val = np.median(arr1d)

# 标准差和方差

std_dev = np.std(arr1d)

variance = np.var(arr1d)

# 沿指定轴求和

sum_axis0 = np.sum(arr2d, axis=0)

sum_axis1 = np.sum(arr2d, axis=1)

5. 查找和搜索

python复制代码

# 非零元素的索引

nonzero_indices = np.nonzero(arr1d)

# 查找特定值的位置

positions = np.where(arr1d == 3)

# 查找唯一值和它们的计数

unique_values, counts = np.unique(arr1d, return_counts=True)

2. pandas

pandas 是一个强大的数据分析工具包,提供了数据结构和数据分析工具,能够处理和分析大量数据。其主要特点包括:

DataFrame:二维的、大小可变的、可以包含异质类型列的表格型数据结构。

Series:一维的、大小可变的、可以包含任何数据类型的数组,以及一组与之相关的数据标签(索引)。

数据读取/写入:可以从各种文件格式(如 CSV、Excel、SQL 数据库等)中读取数据,也可以将数据写入这些格式。

数据处理:提供了数据清洗、转换、合并、重塑等多种功能。

统计分析:提供了各种统计函数和方法。

pandas常用代码

pandas 是 Python 中一个强大的数据分析库,它提供了数据结构(如 DataFrame 和 Series)以及一系列用于数据清洗、转换、分析和可视化的工具。以下是一些 pandas 的常用代码示例:

1. 导入 pandas

python复制代码

import pandas as pd

2. 创建 DataFrame

python复制代码

# 从字典创建 DataFrame

data = {

'Name': ['Alice', 'Bob', 'Charlie'],

'Age': [25, 30, 35],

'City': ['New York', 'San Francisco', 'Los Angeles']

}

df = pd.DataFrame(data)

# 从 CSV 文件读取 DataFrame

df = pd.read_csv('data.csv')

# 从 SQL 数据库读取 DataFrame

# 需要安装 sqlalchemy 和数据库连接库(如 pymysql)

from sqlalchemy import create_engine

engine = create_engine('mysql+pymysql://user:password@localhost:3306/dbname')

df = pd.read_sql_table('table_name', engine)

3. 查看 DataFrame 信息

python复制代码

# 显示前几行

print(df.head())

# 显示后几行

print(df.tail())

# 显示 DataFrame 的结构(列名、数据类型和非空值数量)

print(df.info())

# 显示 DataFrame 的前几行和列的数据类型

print(df.dtypes)

# 显示 DataFrame 的描述性统计信息

print(df.describe())

4. 选择数据

python复制代码

# 选择列

print(df['Age'])

# 选择多列

print(df[['Name', 'Age']])

# 使用 loc 和 iloc 选择行

print(df.loc[0]) # 选择第一行

print(df.iloc[0]) # 同样选择第一行,但基于整数位置

# 基于条件选择行

print(df[df['Age'] > 30])

5. 数据清洗和转换

python复制代码

# 处理缺失值

df.fillna(0, inplace=True) # 将缺失值替换为 0

# 重命名列名

df.rename(columns={'Age': 'Age_Years'}, inplace=True)

# 删除列

df.drop('City', axis=1, inplace=True)

# 删除行

df.drop(df[df['Age_Years'] < 30].index, inplace=True)

# 数据类型转换

df['Age_Years'] = df['Age_Years'].astype(int)

# 字符串操作(例如,将字符串转为大写)

df['Name'] = df['Name'].str.upper()

# 应用函数到 DataFrame 的每个元素

df['Age_Squared'] = df['Age_Years'].apply(lambda x: x**2)

6. 数据分组和聚合

python复制代码

# 使用 groupby 进行分组

grouped = df.groupby('City')

# 对分组后的数据进行聚合(例如,计算每个城市的平均年龄)

agg_result = grouped['Age_Years'].mean()

# 多重聚合

agg_result = grouped.agg({'Age_Years': ['mean', 'count']})

7. 数据排序

python复制代码

# 按列排序

df_sorted = df.sort_values(by='Age_Years')

# 按多列排序

df_sorted_multi = df.sort_values(by=['City', 'Age_Years'])

8. 保存到文件

python复制代码

# 保存到 CSV 文件

df.to_csv('output.csv', index=False)

# 保存到 Excel 文件

df.to_excel('output.xlsx', index=False)

# 保存到 SQL 数据库

df.to_sql('table_name', engine, if_exists='replace', index=False)

3. matplotlib

matplotlib 是一个 Python 2D 绘图库,它提供了类似于 MATLAB 的绘图框架和界面,可以用于绘制各种静态、动态、交互式的可视化图形。其主要特点包括:

简单的绘图语法:类似于 MATLAB 的绘图命令,易于上手。

丰富的图形类型:支持折线图、散点图、柱状图、饼图等多种图形类型。

精细的图形控制:可以控制图形的颜色、线条样式、坐标轴标签等。

交互性:可以与图形进行交互,如放大、缩小、拖动等。

集成性:可以与 numpy、pandas 等库无缝集成,方便地进行数据分析和可视化。

matplotlib 是 Python 中一个非常流行的绘图库,它提供了丰富的绘图功能和接口。以下是一些 matplotlib 的常用代码示例:

1. 导入 matplotlib

python复制代码

import matplotlib.pyplot as plt

2. 绘制折线图

python复制代码

x = [1, 2, 3, 4, 5]

y = [2, 4, 6, 8, 10]

plt.plot(x, y)

plt.title('Line Plot')

plt.xlabel('X Axis')

plt.ylabel('Y Axis')

plt.show()

3. 绘制散点图

python复制代码

x = [1, 2, 3, 4, 5]

y = [2, 3, 5, 7, 11]

plt.scatter(x, y)

plt.title('Scatter Plot')

plt.xlabel('X Axis')

plt.ylabel('Y Axis')

plt.show()

4. 绘制柱状图

python复制代码

x = ['A', 'B', 'C', 'D', 'E']

y = [2, 4, 6, 8, 10]

plt.bar(x, y)

plt.title('Bar Plot')

plt.xlabel('Category')

plt.ylabel('Value')

plt.show()

5. 绘制饼图

python复制代码

labels = ['A', 'B', 'C', 'D', 'E']

sizes = [15, 30, 45, 10, 5]

plt.pie(sizes, labels=labels, autopct='%1.1f%%', startangle=90)

plt.axis('equal') # Equal aspect ratio ensures that pie is drawn as a circle.

plt.show()

6. 绘制直方图

python复制代码

data = [1, 2, 2, 3, 3, 3, 4, 4, 4, 4, 5, 5, 5, 5, 5]

plt.hist(data, bins=5, edgecolor='black')

plt.title('Histogram')

plt.xlabel('Value')

plt.ylabel('Frequency')

plt.show()

7. 绘制多个子图

python复制代码

plt.figure(figsize=(10, 6))

plt.subplot(2, 2, 1) # 2 rows, 2 columns, first plot

plt.plot(x, y)

plt.title('First Plot')

plt.subplot(2, 2, 2) # second plot

plt.scatter(x, y)

plt.title('Second Plot')

plt.subplot(2, 2, 3) # third plot

plt.bar(x, y)

plt.title('Third Plot')

plt.tight_layout() # Adjusts spacing between subplots

plt.show()

8. 添加图例

python复制代码

x = [1, 2, 3, 4, 5]

y1 = [2, 4, 6, 8, 10]

y2 = [3, 5, 7, 9, 11]

plt.plot(x, y1, label='Line 1')

plt.plot(x, y2, label='Line 2')

plt.legend()

plt.title('Line Plot with Legend')

plt.xlabel('X Axis')

plt.ylabel('Y Axis')

plt.show()

9. 自定义颜色、线型等

python复制代码

x = [1, 2, 3, 4, 5]

y = [2, 4, 6, 8, 10]

plt.plot(x, y, color='red', linestyle='--', marker='o')

plt.title('Customized Line Plot')

plt.xlabel('X Axis')

plt.ylabel('Y Axis')

plt.show()

numpy

import numpy as np#创建数组# 一维数组
arr1d = np.array([1, 2, 3, 4, 5])# 二维数组(矩阵)
arr2d = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]])# 使用 zeros, ones, empty 创建特定形状的数组
zeros_arr = np.zeros((3, 3))
ones_arr = np.ones((2, 4))
empty_arr = np.empty((2, 3))  # 注意:内容未初始化,可能是任何值# 使用 arange, linspace 创建一维数组
arange_arr = np.arange(0, 10, 2)  # 从 0 开始,到 10(不包括),步长为 2
linspace_arr = np.linspace(0, 1, 5)  # 从 0 到 1,生成 5 个等间隔的数# 使用 random 创建随机数组
random_arr = np.random.rand(3, 3)  # 生成 0 到 1 之间的随机数
randint_arr = np.random.randint(0, 10, (3, 3))  # 生成 0 到 9 之间的随机整数#数组操作# 数组运算(元素级)
result = arr1d + arr1d  # 对应元素相加
result = arr2d * 2  # 所有元素乘以 2# 索引和切片
element = arr2d[0, 0]  # 获取第一个元素
row = arr2d[1, :]  # 获取第二行
col = arr2d[:, 1]  # 获取第二列# 形状(shape)和大小(size)
shape = arr2d.shape  # 获取形状,例如 (3, 3)
size = arr2d.size  # 获取元素总数# 数据类型(dtype)
dtype = arr1d.dtype  # 获取数据类型,例如 dtype('int64')# 排序
sorted_arr = np.sort(arr1d)# 条件选择
mask = arr1d > 3
selected_elements = arr1d[mask]# 数组重塑(reshape)
reshaped_arr = arr1d.reshape((1, 5))# 连接数组(concatenate)
concat_arr = np.concatenate((arr1d, [6, 7]))# 数组转置(transpose)
transposed_arr = arr2d.T# 矩阵乘法
dot_product = np.dot(arr2d, arr2d.T)#统计和聚合# 最小值、最大值、平均值、中位数等
min_val = np.min(arr1d)
max_val = np.max(arr1d)
mean_val = np.mean(arr1d)
median_val = np.median(arr1d)# 标准差和方差
std_dev = np.std(arr1d)
variance = np.var(arr1d)# 沿指定轴求和
sum_axis0 = np.sum(arr2d, axis=0)
sum_axis1 = np.sum(arr2d, axis=1)#查找和搜索# 非零元素的索引
nonzero_indices = np.nonzero(arr1d)# 查找特定值的位置
positions = np.where(arr1d == 3)# 查找唯一值和它们的计数
unique_values, counts = np.unique(arr1d, return_counts=True)

pandas


import pandas as pd#创建DataFrame和Series# 创建 DataFrame  
data = {'Name': ['Alice', 'Bob', 'Charlie'], 'Age': [25, 30, 35], 'City': ['New York', 'Paris', 'London']}
df = pd.DataFrame(data)# 创建 Series  
s = pd.Series([1, 2, 3, 4], name='Numbers')#读取和写入数据# 读取 CSV 文件  
df = pd.read_csv('data.csv')# 写入 CSV 文件  
df.to_csv('output.csv', index=False)# 读取 Excel 文件  
df = pd.read_excel('data.xlsx')# 写入 Excel 文件  
df.to_excel('output.xlsx', index=False)#选择数据# 选择列  
ages = df['Age']# 选择多列  
info = df[['Name', 'Age']]# 选择行  
first_row = df.iloc[0]  # 使用整数位置  
bob_row = df[df['Name'] == 'Bob']  # 使用条件  # 选择特定行和列  
selected_data = df.loc[df['Age'] > 30, ['Name', 'City']]#数据处理# 对某列应用函数  
df['AgeSquared'] = df['Age'] ** 2# 替换值  
df.replace({'City': {'New York': 'NYC'}}, inplace=True)# 删除列  
df.drop('AgeSquared', axis=1, inplace=True)# 删除行(基于条件)  
df = df[df['Age'] > 20]# 数据排序  
df_sorted = df.sort_values(by='Age')# 数据分组和聚合  
grouped = df.groupby('City')['Age'].mean()#数据合并和连接# 合并两个 DataFrame(基于索引)  
df1 = pd.DataFrame({'A': ['A0', 'A1', 'A2', 'A3'],'B': ['B0', 'B1', 'B2', 'B3'],'key': ['K0', 'K0', 'K1', 'K1'],'C': ['C0', 'C1', 'C2', 'C3']})df2 = pd.DataFrame({'A': ['A4', 'A5', 'A6', 'A7'],'B': ['B4', 'B5', 'B6', 'B7'],'key': ['K0', 'K1', 'K0', 'K1'],'D': ['D0', 'D1', 'D2', 'D3']})merged = pd.merge(df1, df2, on='key')# 连接两个 DataFrame(基于索引)  
concatenated = pd.concat([df1, df2], ignore_index=True)#数据统计# 描述性统计  
stats = df.describe()# 唯一值计数  
unique_counts = df['City'].value_counts()# 空值检查  
null_counts = df.isnull().sum()#数据可视化
# 绘制直方图  
df['Age'].plot(kind='hist', bins=20)# 使用 seaborn 进行更复杂的可视化  
import seaborn as snssns.barplot(x='City', y='Age', data=df)

matplotlib

import matplotlib.pyplot as plt#绘制折线图x = [1, 2, 3, 4, 5]
y = [2, 4, 6, 8, 10]plt.plot(x, y)
plt.title('Line Plot')
plt.xlabel('X Axis')
plt.ylabel('Y Axis')
plt.show()#绘制散点图x = [1, 2, 3, 4, 5]
y = [2, 3, 5, 7, 11]plt.scatter(x, y)
plt.title('Scatter Plot')
plt.xlabel('X Axis')
plt.ylabel('Y Axis')
plt.show()#绘制柱状图x = ['A', 'B', 'C', 'D', 'E']
y = [2, 4, 6, 8, 10]plt.bar(x, y)
plt.title('Bar Plot')
plt.xlabel('Category')
plt.ylabel('Value')
plt.show()#绘制饼图labels = ['A', 'B', 'C', 'D', 'E']
sizes = [15, 30, 45, 10, 5]plt.pie(sizes, labels=labels, autopct='%1.1f%%', startangle=90)
plt.axis('equal')  # Equal aspect ratio ensures that pie is drawn as a circle.
plt.show()#绘制直方图data = [1, 2, 2, 3, 3, 3, 4, 4, 4, 4, 5, 5, 5, 5, 5]plt.hist(data, bins=5, edgecolor='black')
plt.title('Histogram')
plt.xlabel('Value')
plt.ylabel('Frequency')
plt.show()#绘制多个子图plt.figure(figsize=(10, 6))plt.subplot(2, 2, 1)  # 2 rows, 2 columns, first plot
plt.plot(x, y)
plt.title('First Plot')plt.subplot(2, 2, 2)  # second plot
plt.scatter(x, y)
plt.title('Second Plot')plt.subplot(2, 2, 3)  # third plot
plt.bar(x, y)
plt.title('Third Plot')plt.tight_layout()  # Adjusts spacing between subplots
plt.show()#添加图例x = [1, 2, 3, 4, 5]
y1 = [2, 4, 6, 8, 10]
y2 = [3, 5, 7, 9, 11]plt.plot(x, y1, label='Line 1')
plt.plot(x, y2, label='Line 2')
plt.legend()
plt.title('Line Plot with Legend')
plt.xlabel('X Axis')
plt.ylabel('Y Axis')
plt.show()#自定义颜色、线型等x = [1, 2, 3, 4, 5]
y = [2, 4, 6, 8, 10]plt.plot(x, y, color='red', linestyle='--', marker='o')
plt.title('Customized Line Plot')
plt.xlabel('X Axis')
plt.ylabel('Y Axis')
plt.show()

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/840630.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

MPC源码解读及路径跟踪demo

一、前言 上篇文章对MPC原理进行了推导&#xff0c;这篇文章对网上开源代码进行解读及实现路径跟踪demo。 分享一段心灵鸡汤&#xff1a; 最近在读的一本书《杀死一只知更鸟》&#xff0c;里面有这样一段话引起了我的共鸣&#xff1a;”我想让你见识一下什么是真正的勇敢&…

JSON转kotlin数据类的在线工具

toc 以下几个在线工具来将JSON转换为Kotlin数据类&#xff1a; ## 1. **Quicktype** (app.quicktype.io/#lkt)&#xff1a;这是一个在线工具&#xff0c;可以将JSON数据转换为多种编程语言的数据类型&#xff0c;包括Kotlin。您可以将JSON数据复制到Quicktype中&#xff0c;…

html 字体设置 (web端字体设置)

windows自带的字体是有版权的&#xff0c;包括微软雅黑&#xff08;方正&#xff09;、宋体&#xff08;中易&#xff09;、黑体&#xff08;中易&#xff09;等 版权算是个大坑&#xff0c;所谓为了避免版权问题&#xff0c;全部使用开源字体即可 我这里选择的是思源宋体&…

nodejs 与 npm 版本对应关系

官方地址&#xff1a;https://nodejs.org/en/about/previous-releases

fastadmin接口输出图片 自动拼接网站URL

先自定义常量 1.文件接口路径 修改核心文件 application\common\controller\Api.php/*** 构造方法* access public* param Request $request Request 对象*/public function __construct(Request $request null){$this->request is_null($request) ? Request::instance…

C++容器之无序多集(std::unordered_multiset)

目录 1 概述2 使用实例3 接口使用3.1 construct3.2 assigns3.3 iterators3.4 capacity3.5 find3.6 count3.7 equal_range3.8 emplace3.9 emplace_hint3.10 insert3.11 erase3.12 clear3.13 swap3.14 bucket_count3.15 max_bucket_count3.16 bucket_size3.17 bucket3.18 load_fa…

C++|设计模式(四)|代理模式

代理模式属于结构型设计模式&#xff0c;并不关注与对象的产生&#xff0c;而是类和对象功能的使用&#xff1b; 该类模设计模式关注类和对象的组合。继承的概念被用来组合接口和定义组合对象获得新功能的方式。 比如说我们想要访问某公司的老板&#xff0c;其实不可能是直接…

全栈式数据统计:SqlAlchemy怎样连接MsSql Server获取视图列表

1.源代码 #-----------获取数据库视图列表----------------------------- # -------密码含特殊字符使用 from urllib.parse import quote_plus as urlquotefrom sqlalchemy import create_engine, MetaData, inspect# 替换为你的数据库连接字符串 DRIVER "ODBC Driver 1…

c++的查漏补缺 1、函数指针

今天写链表的插入排序时遇到了一个问题 void InsertionSortList(ListNode* head, int n){if (!head||!head->next) return nullptr;auto dummy new ListNode(-1);dummy->next head;auto pre head;auto cur head->next;while (cur ! NULL){auto tmp dummy;if (pre…

【新】snapd申请Let‘s Encrypt免费SSL证书、自动化续签证书

简介 之前写过一篇certbot申请SSL证书的文章&#xff1a;SSL证书申请&#xff0c;写得比较详细&#xff0c;但是最近发现使用snapd会更方便。 使用机器&#xff1a;Ubuntu 20.04 简单步骤 1、首先安装必要软件 sudo apt install snapd sudo apt install certbot sudo apt …

可视化在医疗健康领域的巨大价值,该如何设计呢。

可视化设计在医疗健康领域具有以下价值&#xff1a; 数据展示与分析&#xff1a;可视化设计可以将医疗健康领域的大量数据以图表、图形等形式进行展示和分析&#xff0c;帮助医生、研究人员和决策者更直观地理解和解读数据&#xff0c;发现规律和趋势&#xff0c;从而做出科学决…

有效的完全平方数-力扣

在使用二分法完成题目时&#xff0c;使用如下条件判断时 if(mid < num/mid)当输入 num 5&#xff0c;当二分查找到 mid 2时&#xff0c; 出现了mid num/mid的情况&#xff0c;暴露出了这种判断条件的缺陷。 class Solution { public:bool isPerfectSquare(int num) {i…

力扣 10. 正则表达式匹配 python AC

动态规划 class Solution:def isMatch(self, s, p):s sp psize1 len(p)size2 len(s)dp [[False] * size2 for _ in range(size1)]# p到i和s到j为止&#xff0c;是否匹配dp[0][0] Truefor i in range(size1):for j in range(size2):if p[i] .:if p[i - 1] * and …

跨语言摘要CLS近期论文研究总结(二)

1.BART: Denoising Sequence-to-Sequence Pre-training for Natural Language Generation, Translation, and Comprehension 自然语言生成&#xff0c;翻译和理解的去噪序列到序列预训练 BART的训练方法是: (1)以任意的加噪方式对文本进行破坏 (2)学习一个模型来重建原始文本…

【NOI2010】能量采集 题解

推荐在 cnblogs 上阅读。 【NOI2010】能量采集 题解 谨纪念我的第一道手推出来的莫反题。 题目大意&#xff1a;已知 n n n&#xff0c; m m m&#xff0c;求 ∑ i 1 n ∑ j 1 m ( 2 ⋅ gcd ⁡ ( i , j ) − 1 ) \sum\limits_{i1}^n\sum\limits_{j1}^m(2\cdot \gcd(i,j)…

CMU15-445-并发控制,事务实现

事务并发控制 CMU15-445概览 2PL代表两阶段锁协议&#xff08;Two-phase locking&#xff09;。这是一种并发控制机制&#xff0c;用于关系数据库系统中以保障数据完整性。在这种机制中&#xff0c;事务的执行被划分为两个阶段&#xff1a;加锁阶段和释放锁阶段。加锁阶段发生…

Spring相关知识集锦----2

一、Spring循环依赖三级缓存解决方式 singletonObjects:一级缓存 earlySingletonObjects:二级缓存 singletonFactories:三级缓存 spring如何使用三级缓存解决循环依赖&#xff1a; 1.a实例化完成后&#xff0c;将a放入三级缓存 2.初始化a&#xff0c;又去创建b 3.b实例化…

C++高效死锁检测——实现原理与应用(基于强连通分量)

背景 在项目使用多进程、多线程过程中&#xff0c;因争夺资源而造成一种资源竞态&#xff0c;所以需加锁处理。如下图所示&#xff0c;线程 A 想获取线程 B 的锁&#xff0c;线程 B 想获取线程 C 的锁&#xff0c;线程 C 想获取线程 D 的锁&#xff0c; 线程 D 想获取线程 A 的…

回溯大法总结

前言 本篇博客将分两步来进行&#xff0c;首先谈谈我对回溯法的理解&#xff0c;然后通过若干道题来进行讲解&#xff0c;最后总结 对回溯法的理解 回溯法可以看做蛮力法的升级版&#xff0c;它在解决问题时的每一步都尝试所有可能的选项&#xff0c;最终找出所以可行的方案…