How to print defaultdict variable without its type?
Asked Answered
D

4

21

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?

Dx answered 24/12, 2016 at 19:21 Comment(3)
Why do you need the output to be that? You could just call dict on your defaultdict result confusion_proba_dict to get what you want: dict(confusion_proba_dict). But I still think that is excessive and not necessary.Samale
You're not taking any advantage of the defaultdict class in your code. In fact the values in confusion_proba_dict will end up being integer values after the for loop executes since the float factory function is never invoked. Just create a regular dictionary using confusion_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 printing defaultdicts disappear.Yazzie
@Yazzie if you do it like that, you don't need to the explicit call to float, {i: i + 10.0 for i in range(10)} work the same with the implicit type conversionCarsoncarstensz
C
29

just transform it to a regular dict

print( dict(confusion_proba_dict) )

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

Carsoncarstensz answered 24/12, 2016 at 19:29 Comment(5)
Actually, it's not necessary to convert the defaultdict instance to a dict first. Instead just call the base class dict's method for getting the string representation, but pass it the derived class instance as its self argument. i.e. print( dict.__repr__(confusion_proba_dict) ). Avoiding the conversion will be slightly more efficient, more so obviously for larger dictionaries. This is essentially what @Samale was suggesting.Yazzie
@Yazzie that is interesting, but kind of hacky. About the other point, well the same can be said about Waxrat, the difference is that I explicitly suggest to use a regular dict instead, therefore mine was the accepted answer, I think...Carsoncarstensz
Copperfield: If the hackiness bothers you, the same thing can be done more formally by deriving a subclass of defaultdict whose __repr__() method did essentially the same thing. That approach would take a few additional lines of code, of course, but either way, would avoid the overhead of having to convert the instance temporarily into a regular dictionary.Yazzie
What if confusion_proba_dict is a nested collection where internally it has defaultdict? How would you convert the nested items?Iceblink
@Iceblink in that case is probably better to do a martineau said a make a subclass of defaultdict that print how you want and use that insteadCarsoncarstensz
P
9

Funny solution ! ( works also if you have defaultdict of defaultdict ! )

import json
import pprint
pprint.pprint(json.loads(json.dumps(confusion_proba_dict)))
Priorate answered 31/10, 2019 at 13:52 Comment(1)
This is actually a great solution for a nested defaultdict of defaultdict.Iceblink
A
6

Python 2:

print dict(confusion_proba_dict)

Python 3:

print(dict(confusion_proba_dict))
Affective answered 24/12, 2016 at 19:24 Comment(1)
Note for Python 3 users who might come across this question and look at the answer. You need to wrap with parentheses: print(dict(confusion_proba_dict)), since print is no longer a statement in Python 3.Samale
D
0

Convert into json

print(json.dumps(confusion_proba_dict, indent=4))
Dorsy answered 24/7 at 9:22 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.