【发布时间】:2020-07-21 18:10:03
【问题描述】:
我正在参加一些 Kaggle 认证课程。在中级机器学习教程中,我碰到了这个 for 循环。我知道 for 循环是如何工作的,但是这个 for 循环是不同的。
# Get names of columns with missing values
cols_with_missing = [col for col in X_train.columns
if X_train[col].isnull().any()]
# Drop columns in training and validation data
reduced_X_train = X_train.drop(cols_with_missing, axis=1)
reduced_X_valid = X_valid.drop(cols_with_missing, axis=1)
你可以在 col_with_missing 变量中看到。
-
for 循环在括号内做什么以及为什么在 for 语句之前调用 col。
-
如果我们在 if 语句中调用 X_train[col] 数据,那么 reduced_X_valid 变量是如何工作的,因为它得到了错误的数据。
【问题讨论】:
-
它被称为列表理解。
标签: python python-3.x python-2.7