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?
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?
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'])]