Python Frozenset

时间:2014-12-06 06:20:50

标签: python string set frozenset

我正在使用frozenset,我想避免包含'frozenset'的输出。例如,我有

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

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

有什么想法吗?

1 个答案:

答案 0 :(得分:1)

您可以通过创建frozenset的子类并覆盖其__repr__方法来执行此操作:

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