In the following code:
from collections import defaultdict
confusion_proba_dict = defaultdict(float)
for i in xrange(10):
confusion_proba_dict[i] = i + 10
print confusion_proba_dict
Output is:
defaultdict(<type 'float'>, {0: 10, 1: 11, 2: 12, 3: 13, 4: 14, 5: 15, 6: 16, 7: 17, 8: 18, 9: 19})
But, I need output to be:
{0: 10, 1: 11, 2: 12, 3: 13, 4: 14, 5: 15, 6: 16, 7: 17, 8: 18, 9: 19}
How can I do it?
dict
on your defaultdict resultconfusion_proba_dict
to get what you want:dict(confusion_proba_dict)
. But I still think that is excessive and not necessary. – Samaledefaultdict
class in your code. In fact the values inconfusion_proba_dict
will end up being integer values after thefor
loop executes since thefloat
factory function is never invoked. Just create a regular dictionary usingconfusion_proba_dict = {i: float(i) + 10 for i in range(10)}
. The result will be{0: 10.0, 1: 11.0, 2: 12.0, 3: 13.0, 4: 14.0, 5: 15.0, 6: 16.0, 7: 17.0, 8: 18.0, 9: 19.0}
and it will make the whole issue about printingdefaultdict
s disappear. – Yazzie{i: i + 10.0 for i in range(10)}
work the same with the implicit type conversion – Carsoncarstensz