无法在列表理解中索引字典

时间:2016-04-01 02:59:07

标签: dictionary scikit-learn list-comprehension python-3.5

我试图从list list中提取dict。 列表中的每个dict都有一个键值映射。我想在list包含的每个dict中创建一个list个单值。

请参阅下面的代码和输出,以交互式形式编写。

>>> print(str(param_to_outputs_dict_dict))
{('max_features', 1558): {'classifier': DecisionTreeClassifier(class_weight=None, criterion='gini', max_depth=None,
        max_features=1558, max_leaf_nodes=None, min_samples_leaf=1,
        min_samples_split=2, min_weight_fraction_leaf=0.0,
        presort=False, random_state=None, splitter='best')}, 
('max_features', 7713): {'classifier': DecisionTreeClassifier(class_weight=None, criterion='gini', max_depth=None,
        max_features=7713, max_leaf_nodes=None, min_samples_leaf=1,
        min_samples_split=2, min_weight_fraction_leaf=0.0,
        presort=False, random_state=None, splitter='best')}
>>> print()

上面我们看到param_to_outputs_dict_dict是两个条目的dict。每个条目的关键字为tuple,值为dict

接下来,我使用列表推导来提取list值(类型为dict

>>> map_list = [output_dict for output_dict in list(param_to_outputs_dict_dict.values())]
>>> print(str(map_list))
[{'classifier': DecisionTreeClassifier(class_weight=None, criterion='gini', max_depth=None,
        max_features=1558, max_leaf_nodes=None, min_samples_leaf=1,
        min_samples_split=2, min_weight_fraction_leaf=0.0,
        presort=False, random_state=None, splitter='best')}, 
{'classifier': DecisionTreeClassifier(class_weight=None, criterion='gini', max_depth=None,
        max_features=7713, max_leaf_nodes=None, min_samples_leaf=1,
        min_samples_split=2, min_weight_fraction_leaf=0.0,
        presort=False, random_state=None, splitter='best')}]
>>> print()

我们也看到了这种方法。我们有一个list包含两个dict s,它们是param_to_outputs_dict_dict中包含的值。

现在,我没有提取字典,而是尝试提取值'classifier'映射的每个字典中的值。

>>> classifier_list = [output_dict['classifier'] for output_dict in list(param_to_outputs_dict_dict.values())]
>>> print(str(classifier_list))


---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-176-b6f236339c69> in <module>()
      8 print(str(map_list))
      9 print()
---> 10 classifier_list = [output_dict['classifier'] for output_dict in list(param_to_outputs_dict_dict.values())]
     11 print(str(classifier_list))
     12 print()

<ipython-input-176-b6f236339c69> in <listcomp>(.0)
      8 print(str(map_list))
      9 print()
---> 10 classifier_list = [output_dict['classifier'] for output_dict in list(param_to_outputs_dict_dict.values())]
     11 print(str(classifier_list))
     12 print()
TypeError: 'DecisionTreeClassifier' object is not subscriptable

然而,这不起作用。错误消息让我认为Python将output_dict解释为DecisionTreeClassifier类型,但我不明白为什么。我已经玩了好几个小时这个问题,但我完全被难倒了。

我怀疑这个解决方案对其他人来说可能非常明显!提前谢谢!

编辑:原始帖子中缺少重要信息

为了使问题更简洁,更清晰,我将打印结果截断为仅包含任何集合的前两个元素。但是,在意识到问题后(参见下面的答案),当给出完整列表时,问题的原因非常明显。见下文,特别是列表的最后一个元素。

>>> for value in list(param_to_outputs_dict_dict.values()):
>>>     print(str(value))
{'classifier': DecisionTreeClassifier(class_weight=None, criterion='gini', max_depth=None,
        max_features=None, max_leaf_nodes=None, min_samples_leaf=1,
        min_samples_split=2, min_weight_fraction_leaf=0.0,
        presort=False, random_state=None, splitter='best')}
{'classifier': DecisionTreeClassifier(class_weight=None, criterion='gini', max_depth=10,
        max_features=None, max_leaf_nodes=None, min_samples_leaf=1,
        min_samples_split=2, min_weight_fraction_leaf=0.0,
        presort=False, random_state=None, splitter='best')}
{'classifier': DecisionTreeClassifier(class_weight=None, criterion='gini', max_depth=100,
        max_features=None, max_leaf_nodes=None, min_samples_leaf=1,
        min_samples_split=2, min_weight_fraction_leaf=0.0,
        presort=False, random_state=None, splitter='best')}
{'classifier': DecisionTreeClassifier(class_weight=None, criterion='gini', max_depth=None,
        max_features=None, max_leaf_nodes=-100, min_samples_leaf=1,
        min_samples_split=2, min_weight_fraction_leaf=0.0,
        presort=False, random_state=None, splitter='best')}
{'classifier': DecisionTreeClassifier(class_weight=None, criterion='gini', max_depth=None,
        max_features=3, max_leaf_nodes=None, min_samples_leaf=1,
        min_samples_split=2, min_weight_fraction_leaf=0.0,
        presort=False, random_state=None, splitter='best')}
{'classifier': DecisionTreeClassifier(class_weight=None, criterion='gini', max_depth=None,
        max_features=None, max_leaf_nodes=-10000, min_samples_leaf=1,
        min_samples_split=2, min_weight_fraction_leaf=0.0,
        presort=False, random_state=None, splitter='best')}
{'classifier': DecisionTreeClassifier(class_weight=None, criterion='gini', max_depth=None,
        max_features=7791, max_leaf_nodes=None, min_samples_leaf=1,
        min_samples_split=2, min_weight_fraction_leaf=0.0,
        presort=False, random_state=None, splitter='best')}
{'classifier': DecisionTreeClassifier(class_weight=None, criterion='gini', max_depth=None,
        max_features=77, max_leaf_nodes=None, min_samples_leaf=1,
        min_samples_split=2, min_weight_fraction_leaf=0.0,
        presort=False, random_state=None, splitter='best')}
{'classifier': DecisionTreeClassifier(class_weight=None, criterion='gini', max_depth=100000,
        max_features=None, max_leaf_nodes=None, min_samples_leaf=1,
        min_samples_split=2, min_weight_fraction_leaf=0.0,
        presort=False, random_state=None, splitter='best')}
{'classifier': DecisionTreeClassifier(class_weight=None, criterion='gini', max_depth=None,
        max_features=1558, max_leaf_nodes=None, min_samples_leaf=1,
        min_samples_split=2, min_weight_fraction_leaf=0.0,
        presort=False, random_state=None, splitter='best')}
{'classifier': DecisionTreeClassifier(class_weight=None, criterion='gini', max_depth=1,
        max_features=None, max_leaf_nodes=None, min_samples_leaf=1,
        min_samples_split=2, min_weight_fraction_leaf=0.0,
        presort=False, random_state=None, splitter='best')}
{'classifier': DecisionTreeClassifier(class_weight=None, criterion='gini', max_depth=None,
        max_features=3895, max_leaf_nodes=None, min_samples_leaf=1,
        min_samples_split=2, min_weight_fraction_leaf=0.0,
        presort=False, random_state=None, splitter='best')}
{'classifier': DecisionTreeClassifier(class_weight=None, criterion='gini', max_depth=None,
        max_features=7713, max_leaf_nodes=None, min_samples_leaf=1,
        min_samples_split=2, min_weight_fraction_leaf=0.0,
        presort=False, random_state=None, splitter='best')}
{'classifier': DecisionTreeClassifier(class_weight=None, criterion='gini', max_depth=None,
        max_features=None, max_leaf_nodes=None, min_samples_leaf=1,
        min_samples_split=2, min_weight_fraction_leaf=0.0,
        presort=False, random_state=None, splitter='best')}
{'classifier': DecisionTreeClassifier(class_weight=None, criterion='gini', max_depth=None,
        max_features=7011, max_leaf_nodes=None, min_samples_leaf=1,
        min_samples_split=2, min_weight_fraction_leaf=0.0,
        presort=False, random_state=None, splitter='best')}
{'classifier': DecisionTreeClassifier(class_weight='balanced', criterion='gini',
        max_depth=None, max_features=None, max_leaf_nodes=None,
        min_samples_leaf=1, min_samples_split=2,
        min_weight_fraction_leaf=0.0, presort=False, random_state=None,
        splitter='best')}
{'classifier': DecisionTreeClassifier(class_weight=None, criterion='gini', max_depth=None,
        max_features=None, max_leaf_nodes=None, min_samples_leaf=1,
        min_samples_split=2, min_weight_fraction_leaf=0.0,
        presort=False, random_state=None, splitter='random')}
{'classifier': DecisionTreeClassifier(class_weight=None, criterion='gini', max_depth=None,
        max_features=None, max_leaf_nodes=-1000, min_samples_leaf=1,
        min_samples_split=2, min_weight_fraction_leaf=0.0,
        presort=False, random_state=None, splitter='best')}
{'classifier': DecisionTreeClassifier(class_weight=None, criterion='gini', max_depth=None,
        max_features=779, max_leaf_nodes=None, min_samples_leaf=1,
        min_samples_split=2, min_weight_fraction_leaf=0.0,
        presort=False, random_state=None, splitter='best')}
{'classifier': DecisionTreeClassifier(class_weight=None, criterion='gini', max_depth=10000,
        max_features=None, max_leaf_nodes=None, min_samples_leaf=1,
        min_samples_split=2, min_weight_fraction_leaf=0.0,
        presort=False, random_state=None, splitter='best')}
{'classifier': DecisionTreeClassifier(class_weight=None, criterion='gini', max_depth=None,
        max_features=None, max_leaf_nodes=-1, min_samples_leaf=1,
        min_samples_split=2, min_weight_fraction_leaf=0.0,
        presort=False, random_state=None, splitter='best')}
{'classifier': DecisionTreeClassifier(class_weight=None, criterion='gini', max_depth=None,
        max_features=None, max_leaf_nodes=1000, min_samples_leaf=1,
        min_samples_split=2, min_weight_fraction_leaf=0.0,
        presort=False, random_state=None, splitter='best')}
{'classifier': DecisionTreeClassifier(class_weight=None, criterion='gini', max_depth=None,
        max_features=None, max_leaf_nodes=10, min_samples_leaf=1,
        min_samples_split=2, min_weight_fraction_leaf=0.0,
        presort=False, random_state=None, splitter='best')}
{'classifier': DecisionTreeClassifier(class_weight=None, criterion='gini', max_depth=1000,
        max_features=None, max_leaf_nodes=None, min_samples_leaf=1,
        min_samples_split=2, min_weight_fraction_leaf=0.0,
        presort=False, random_state=None, splitter='best')}
{'classifier': DecisionTreeClassifier(class_weight=None, criterion='gini', max_depth=None,
        max_features=None, max_leaf_nodes=10000, min_samples_leaf=1,
        min_samples_split=2, min_weight_fraction_leaf=0.0,
        presort=False, random_state=None, splitter='best')}
{'classifier': DecisionTreeClassifier(class_weight=None, criterion='gini', max_depth=None,
        max_features=None, max_leaf_nodes=None, min_samples_leaf=1,
        min_samples_split=2, min_weight_fraction_leaf=0.0,
        presort=False, random_state=None, splitter='best')}
{'classifier': DecisionTreeClassifier(class_weight=None, criterion='gini', max_depth=None,
        max_features=6232, max_leaf_nodes=None, min_samples_leaf=1,
        min_samples_split=2, min_weight_fraction_leaf=0.0,
        presort=False, random_state=None, splitter='best')}
{'classifier': DecisionTreeClassifier(class_weight=None, criterion='gini', max_depth=None,
        max_features=None, max_leaf_nodes=100, min_samples_leaf=1,
        min_samples_split=2, min_weight_fraction_leaf=0.0,
        presort=False, random_state=None, splitter='best')}
{'classifier': DecisionTreeClassifier(class_weight=None, criterion='entropy', max_depth=None,
        max_features=None, max_leaf_nodes=None, min_samples_leaf=1,
        min_samples_split=2, min_weight_fraction_leaf=0.0,
        presort=False, random_state=None, splitter='best')}
{'classifier': DecisionTreeClassifier(class_weight=None, criterion='gini', max_depth=None,
        max_features=88, max_leaf_nodes=None, min_samples_leaf=1,
        min_samples_split=2, min_weight_fraction_leaf=0.0,
        presort=False, random_state=None, splitter='best')}
{'classifier': DecisionTreeClassifier(class_weight=None, criterion='gini', max_depth=None,
        max_features=None, max_leaf_nodes=-10, min_samples_leaf=1,
        min_samples_split=2, min_weight_fraction_leaf=0.0,
        presort=False, random_state=None, splitter='best')}
{'classifier': DecisionTreeClassifier(class_weight=None, criterion='gini', max_depth=None,
        max_features=None, max_leaf_nodes=None, min_samples_leaf=1,
        min_samples_split=2, min_weight_fraction_leaf=0.0,
        presort=False, random_state=None, splitter='best')}
{'classifier': DecisionTreeClassifier(class_weight=None, criterion='gini', max_depth=None,
        max_features=None, max_leaf_nodes=100000, min_samples_leaf=1,
        min_samples_split=2, min_weight_fraction_leaf=0.0,
        presort=False, random_state=None, splitter='best')}
{'classifier': DecisionTreeClassifier(class_weight=None, criterion='gini', max_depth=None,
        max_features=None, max_leaf_nodes=-100000, min_samples_leaf=1,
        min_samples_split=2, min_weight_fraction_leaf=0.0,
        presort=False, random_state=None, splitter='best')}
DecisionTreeClassifier(class_weight=None, criterion='gini', max_depth=None,
        max_features=None, max_leaf_nodes=None, min_samples_leaf=1,
        min_samples_split=2, min_weight_fraction_leaf=0.0,
        presort=False, random_state=None, splitter='best')

2 个答案:

答案 0 :(得分:0)

查看问题中打印的完整列表,我们可以看到最后一个元素不是包含dictstr映射的DecisionTreeClassifier类型,而是仅仅是输入DecisionTreeClassifier

因此,以下代码:

[output_dict['classifier'] for output_dict in list(param_to_outputs_dict_dict.values())]

list(param_to_outputs_dict_dict.values())的每个元素分配给output_dict,在最后一个元素处分配DecisionTreeClassifier

因此,在该迭代中,语句output_dict['classifier']尝试下标DecisionTreeClassifier个对象,一个dict。这会产生问题中给出的TypeError

答案 1 :(得分:0)

你正在做一些多余的事情。以下是这样做的方法。

[v["classifier"] for v in param_to_outputs_dict_dict.values()]

list(dict.values())[v for v in dict.values()]是一回事,那么[v for v in list(dict.values())]的语义理由是什么。在同一行,print(str(x))究竟是什么? print(x)输出x.__str__()。这就是我说冗余时的意思。