-
Notifications
You must be signed in to change notification settings - Fork 1
Open
Labels
Description
nestedList = [4,[1,2,[3,5,6]],[4,3,[1,2,[4,5]],2],[1,2,4,5,7]]
print(nestedList)
def enumList(nestedList):
try:
for subList in nestedList:
for element in enumList(subList):
yield element
except TypeError:
yield nestedList # 迭代单个值
for num in enumList(nestedList):
print(num, end=' ')
print(list(enumList(nestedList)))
递归生成器的编写方法与递归函数类似,只是需要处理元素值的时候需要使用yield
关键字