关于递归和生成器的def genflatten(lists):for s in lists:if isinstance(s,list):for item in genflatten
关于递归和生成器的
def genflatten(lists): for s in lists: if isinstance(s,list): for item in genflatten(s): yield item else: yield item q=[[1,2,3],[4,5,[5,6]],[7,8,9]] list(genflatten(q))
运行后出现错误 UnboundLocalError: local variable 'item' referenced before assignment
求解 [解决办法] 好烂的递归, 这样:
def genflatten(lists): if isinstance(lists, list): for sub_lists in lists: for item in genflatten(sub_lists): yield item else: yield lists