Skip to content

Instantly share code, notes, and snippets.

@licsber
Created May 29, 2020 07:26
Show Gist options
  • Select an option

  • Save licsber/6e0f2d4172da36018b85c51fd6910da7 to your computer and use it in GitHub Desktop.

Select an option

Save licsber/6e0f2d4172da36018b85c51fd6910da7 to your computer and use it in GitHub Desktop.
python 平展 语法糖演示
b = [[1, 2], [3], [[1, [2]]]]
flatten1 = lambda x: [y for l in x for y in flatten1(l)] if type(x) is list else [x]
def flatten2(x):
res = []
if type(x) is list:
for l in x:
for y in flatten2(l):
res.append(y)
else:
res = [x]
return res
print(flatten1(b))
print(flatten2(b))
if __name__ == '__main__':
pass
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment