def flatten(nstd_list):
for item in nstd_list:
try:
yield from flatten(item)
except TypeError:
yield item
I am a beginner for python, can you here please explain me how does this work(step by step)
def flatten(nstd_list):
for item in nstd_list:
try:
yield from flatten(item)
except TypeError:
yield item
I am a beginner for python, can you here please explain me how does this work(step by step)
you can take yield
as return
, so the code is to get every single element from a nested list.
for example:
nstd_list = [[1],2]
first round: item is [1] and 2, so yield flatten([1])
and 2
second round: item is 1, and return 1