此
traverse生成器函数可用于遍历所有值:
def traverse(o, tree_types=(list, tuple)): if isinstance(o, tree_types): for value in o: for subvalue in traverse(value, tree_types): yield subvalue else: yield odata = [(1,1,(1,1,(1,"1"))),(1,1,1),(1,),1,(1,(1,("1",)))]print list(traverse(data))# prints [1, 1, 1, 1, 1, '1', 1, 1, 1, 1, 1, 1, 1, '1']for value in traverse(data): print repr(value)# prints# 1# 1# 1# 1# 1# '1'# 1# 1# 1# 1# 1# 1# 1# '1'
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)