我正在使用frozenset,我想避免包含“frozenset”的输出。例如,我有
x = [frozenset([item]) for item in Set]
Output: frozenset(['yes']) => frozenset(['red', 'blue'])有什么想法吗?
发布于 2014-12-06 06:47:26
可以通过创建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'])]https://stackoverflow.com/questions/27328950
复制相似问题