本文介绍了如何在没有类型的情况下打印defaultdict变量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在以下代码中:
from collections import defaultdict
confusion_proba_dict = defaultdict(float)
for i in xrange(10):
confusion_proba_dict[i] = i + 10
print confusion_proba_dict
输出为:
defaultdict(<type 'float'>, {0: 10, 1: 11, 2: 12, 3: 13, 4: 14, 5: 15, 6: 16, 7: 17, 8: 18, 9: 19})
但是,我需要输出为:
{0: 10, 1: 11, 2: 12, 3: 13, 4: 14, 5: 15, 6: 16, 7: 17, 8: 18, 9: 19}
我该怎么办?
推荐答案
只需将其转换为常规字典
just transform it to a regular dict
print( dict(confusion_proba_dict) )
但是如果您要这样使用,请使用常规dict,因为您不会使用defaultdict的任何优势
but if you are going to use like that, just use a regular dict as you don't use any of the advantages of defaultdict
这篇关于如何在没有类型的情况下打印defaultdict变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!