0

I am using frozenset and I would like to avoid the output containing 'frozenset'. For example, I have

x = [frozenset([item]) for item in Set]

Output: frozenset(['yes']) => frozenset(['red', 'blue'])

Any ideas?

Ashwini Chaudhary
  • 217,951
  • 48
  • 415
  • 461
user3318660
  • 293
  • 1
  • 3
  • 17

1 Answers1

1

You can do this by creating a subclass of frozenset and overriding its __repr__ method:

class MyFrozenSet(frozenset):
    def __repr__(self):
        return '([{}])'.format(', '.join(map(repr, self)))
...     
>>> lst = [['yes'], ['red', 'blue']]
>>> [MyFrozenSet(x) for x in lst]
[(['yes']), (['blue', 'red'])]
Ashwini Chaudhary
  • 217,951
  • 48
  • 415
  • 461
  • 3
    you don't need a class to do - using a class seems overkill to me. and overriding `__repr__` like this breaks the semantics of what `__repr__` is expected to do. – Tony Suffolk 66 Dec 06 '14 at 08:13
  • 1
    *"If at all possible, [`__repr__`] should look like a valid Python expression that could be used to recreate an object with the same value (given an appropriate environment). If this is not possible, a string of the form `<...some description...="" useful="">` should be returned."* – jonrsharpe Dec 06 '14 at 09:25