如何将Google Colab中的长代码拆分为多行?
长代码拆分及变量名修正方案
首先,你的变量名4y不符合Python语法规则——变量名不能以数字开头,运行时会触发语法错误,建议改为y4或其他合法名称(比如selected_data)。
针对长列表的拆分需求,Python支持在方括号内直接换行,只要保持元素用逗号分隔即可,这种方式可读性最强,示例如下:
# 修正变量名并拆分代码 y4 = bio_fix_drop[ [ 'Radius_mean', 'Radius_mean', 'Texture_mean', 'perimeter_mean', 'area_mean', 'smoothness_mean', 'compactness_mean', 'concavity_mean', 'concave points_mean', 'symmetry_mean', 'fractal_dimension_mean', 'radius_se', 'texture_se', 'perimeter_se', 'area_se', 'smoothness_se', 'compactness_se', 'concavity_se', 'concave points_se', 'symmetry_se', 'fractal_dimension_se', 'radius_worst', 'texture_worst', 'perimeter_worst', 'area_worst', 'smoothness_worst', 'compactness_worst', 'concavity_worst', 'concave points_worst', 'symmetry_worst', 'fractal_dimension_worst', 'diagnosis' ] ]
修改完成后,直接执行print(y4)即可正常输出变量内容。
另外也可以使用反斜杠\换行,但这种方式可读性较差,不推荐:
y4 = bio_fix_drop[\ ['Radius_mean', 'Radius_mean', 'Texture_mean', \ 'perimeter_mean', 'area_mean', 'smoothness_mean', \ 'compactness_mean', 'concavity_mean', 'concave points_mean', \ 'symmetry_mean', 'fractal_dimension_mean', 'radius_se', \ 'texture_se', 'perimeter_se', 'area_se', 'smoothness_se', \ 'compactness_se', 'concavity_se', 'concave points_se', \ 'symmetry_se', 'fractal_dimension_se', 'radius_worst', \ 'texture_worst', 'perimeter_worst', 'area_worst', \ 'smoothness_worst', 'compactness_worst', 'concavity_worst', \ 'concave points_worst', 'symmetry_worst', 'fractal_dimension_worst', \ 'diagnosis']\ ]
内容的提问来源于stack exchange,提问作者Hasif
相关产品推荐
相关产品推荐

