import collections
def flatten(lst):
for item in lst:
if isinstance(item,collections.Iterable)and not isinstance(item,(str,bytes)):
yield from flatten(item)
else:
yield item
例子:
rr = ["1","2d","网址",["see你们","5"]]
for i in flatten(rr):
print(i)
python中嵌套列表的拆分方法import collectionsdef flatten(lst): for item in lst: if isinstance(item,collections.Iterable)and not isinstance(item,(str,bytes)): yield from flatten(item) ...