Created
November 19, 2016 17:47
-
-
Save toanant/b1f30a49cbce57d7b7ab85ace5b30cd1 to your computer and use it in GitHub Desktop.
This file contains 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
import collections | |
def generate_flatten(container, contaianer_types=collections.Sequence): | |
container_list = list(container) | |
while container_list: | |
while container_list and isinstance( | |
container_list[0], contaianer_types): | |
container_list[0:1] = container_list[0] | |
if container_list: | |
yield container_list.pop(0) | |
def test_generate_flatten(): | |
flatten_container = list(generate_flatten([[1, 2, [3]], 4])) | |
assert flatten_container == [1, 2, 3, 4] | |
if __name__ == '__main__': | |
test_generate_flatten() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment