python - 在一个对象中打印所有属性的类型 - 最好的方法?
问题描述
我找到了这篇文章,它回答了这个问题,但我认为应该有一种更简单的方法来做到这一点。
这是我的示例:从对象 boston,我想知道属性是字符串还是数组。a) 给我名字。b) 显然不是我想要的信息 c) 给了我想要的信息 但我认为选项 c) 过于复杂,应该有更清洁的方法来做到这一点。有任何想法吗?
from sklearn.datasets import *
boston = load_boston()
a= dir(boston)
b=[type(x) for x in boston]
c=[type(getattr(boston, name)).__name__ for name in dir(boston) ]
print(a,"\n", b,"\n",c)
出去:
['DESCR', 'data', 'feature_names', 'filename', 'target']
[<class 'str'>, <class 'str'>, <class 'str'>, <class 'str'>, <class 'str'>]
['str', 'ndarray', 'ndarray', 'str', 'ndarray']
解决方案
print([x for x in boston])
print([type(boston[x]) for x in boston])
出去:
['data', 'target', 'feature_names', 'DESCR', 'filename']
[<class 'numpy.ndarray'>, <class 'numpy.ndarray'>, <class 'numpy.ndarray'>, <class 'str'>, <class 'str'>]
推荐阅读
- ios - 如何在不改变其位置的情况下在界面构建器中将子视图置于前面
- kotlin - Camel:当项目数小于批量大小时如何拆分然后聚合
- javascript - 使用函数 .map() 更新状态是变异状态?
- node.js - 无法从“src\containers\Login.js”解析“firebase”
- html - 映射onclick禁用父div的滚动条
- php - TCPDF 问题:如何对齐文本和图像?
- ruby-on-rails - 在范围内使用关联域进行计算
- alphabet - ∑和∑1的区别
- spring - 混合参数策略 - 仅使用命名、位置或 JPA 序数策略之一
- c# - 在 c# 中获取属性的调用者信息