python - Convert sets to frozensets as values of a dictionary -
I have a dictionary that has been created as part of the beginning of my object, I know that the life of this object The key to setting the map will not change during. I want to convert all the values from set
to frozenset
s, to make sure that they can not be changed. Currently I do this:
The key for the self My_dict.iterkeys (): self.my_dict [key] = frozenset (self.my_dict [key]) Is this an easy way to gain? I can not immediately create frozenset
, because I do not know how many items will be in each set unless I make the whole dictionary.
For example,
& gt; & Gt; & Gt; D = {'A': Set ([1, 2]), 'B': set ([3, 4])}> gt; & Gt; & Gt; D {'A': Set ([1, 2]), 'B': set ([3, 4])}
You can do this as a conversion
Are> & gt; & Gt; & Gt; With the result
& gt; & Gt; ; & Gt; D {'a': frozenset ([1, 2]), 'b': frozenset ([3, 4])}
Comments
Post a Comment