Python编程语言学习:利用locals函数判断某个变量参数之前是否已经被定义/存在/出现

Python编程语言学习:利用locals函数判断某个变量参数之前是否已经被定义/存在/出现

 

 

 

目录

利用locals函数判断某个变量参数之前是否已经被定义/存在/出现

输出结果

实现代码


 

 

 

利用locals函数判断某个变量参数之前是否已经被定义/存在/出现

输出结果

<class 'pandas.core.frame.DataFrame'>
RangeIndex: 768 entries, 0 to 767
Data columns (total 9 columns):
 #   Column                    Non-Null Count  Dtype  
---  ------                    --------------  -----  
 0   Pregnancies               768 non-null    int64  
 1   Glucose                   768 non-null    int64  
 2   BloodPressure             768 non-null    int64  
 3   SkinThickness             768 non-null    int64  
 4   Insulin                   768 non-null    int64  
 5   BMI                       768 non-null    float64
 6   DiabetesPedigreeFunction  768 non-null    float64
 7   Age                       768 non-null    int64  
 8   Outcome                   768 non-null    int64  
dtypes: float64(2), int64(7)
memory usage: 54.1 KB
None
dict_keys(['__name__', '__doc__', '__package__', '__loader__', '__spec__', '__annotations__', '__builtins__', '__file__', '__cached__', 'plt', 'pd', 'data_frame', 'col_label', 'cols_other', 'data_X', 'data_y_label_μ'])
data_X
data_y_label_μ
data_dall02
data_dall02 not in locals().keys()!

 

 

 

实现代码

# Python编程语言学习:利用locals函数判断某个变量参数之前是否已经被定义/存在/出现
import pandas as pd

data_frame=pd.read_csv('data_csv_xls\diabetes\diabetes.csv')

col_label='Outcome'
cols_other=['Pregnancies','Glucose','BloodPressure','SkinThickness','BMI']
data_X=data_frame[cols_other]
data_y_label_μ=data_frame[col_label]



# 判断某个参数之前是否已经被定义/存在/出现
print(locals().keys())
param_lists=['data_X','data_y_label_μ','data_dall02']
for param in param_lists:
    print(param)
    if param not in locals().keys():
        print('%s not in locals().keys()!'%param)

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

一个处女座的程序猿

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值