您需要将要打印的项目子类化。
from itertools import chainclass PrintableList(list): # for a list of dicts def __str__(self): return '. '.join(' '.join(str(x) for x in chain.from_iterable(zip((item[0], 'is', 'and'), item[1]))) for item in (item.items()[0] for item in self)) + '.'class PrintableDict(dict): # for a dict def __str__(self): return '. '.join(' '.join(str(x) for x in chain.from_iterable(zip((item[0], 'is', 'and'), item[1]))) for item in self.iteritems()) + '.'class Foo: def __init__(self): self.d = PrintableDict({"Susan": ("Boyle", 50, "alive"), "Albert": ("Speer", 106, "dead")})class Bar: def __init__(self): self.l = PrintableList([{"Susan": ("Boyle", 50, "alive")}, {"Albert": ("Speer", 106, "dead")}])foo = Foo()print self.dbar = Bar()print self.l
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)