【问题标题】:Python3 to find number of features in picklePython3查找pickle中的功能数量
【发布时间】:2019-08-03 04:38:25
【问题描述】:

我读过这个适用于 Python 2 的问题,Number of features in dictionary

然后我按照这个问题的解决方案,但它不起作用,TypeError: 'dict_keys' object does not support indexing

如何查找每个泡菜对象中的特征数量

这里是 github 存储库(如果需要),https://github.com/udacity/ud120-projects

/datasets_questions/explore_enron_data.py的修改代码

import pickle
import pprint

enron_data = pickle.load(open("../final_project/final_project_dataset.pkl", "rb"))
pp=pprint.PrettyPrinter()
pp.pprint(enron_data)
print("Number of people:",len(enron_data))

no_of_features = len(list(enron_data.keys()))  

print("Number of features:",no_of_features)

我希望得到以下输出

Number of people: 146
Number of features: 21

这就是我得到的代替

Number of people: 146
Number of features: 146

【问题讨论】:

    标签: python python-3.x dictionary pickle


    【解决方案1】:

    如果您打印所有 enron_data,您将拥有

    {'METTS MARK': {'salary': 365788, 'to_messages': 807, 'deferral_payments': 'NaN', //etc
    

    如您所见,您将名称作为键,将 {features} 作为值。这些特性也有它们自己的键和值。因此,您不能只计算 enron_data.keys() 的长度。你最好

    print(len(enron_data['METTS MARK']))
    

    输出是

    21
    

    【讨论】:

      猜你喜欢
      • 2022-07-26
      • 2021-12-20
      • 2021-03-04
      • 2013-10-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-02-09
      • 1970-01-01
      相关资源
      最近更新 更多