Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
我正在使用frozenset,我想避免包含“frozenset”的输出。例如,我有
x = [frozenset([item]) for item in Set] Output: frozenset(['yes']) => frozenset(['red', 'blue'])
有任何想法吗?
您可以通过创建一个子类frozenset并覆盖其__repr__方法来做到这一点:
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'])]