Created
May 29, 2020 07:26
-
-
Save licsber/6e0f2d4172da36018b85c51fd6910da7 to your computer and use it in GitHub Desktop.
python 平展 语法糖演示
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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