Created
September 12, 2014 18:31
-
-
Save rpq/ee22505fb1c7f1187551 to your computer and use it in GitHub Desktop.
compact and flatten python
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
20 def compact_array(list_, i=0): | |
21 if i == len(list_): | |
22 return [] | |
23 if not list_[i]: | |
24 return [] + compact_array(list_, i + 1) | |
25 return [list_[i]] + compact_array(list_, i + 1) | |
26 | |
27 def flatten_array(flatten): | |
28 def has_array(list_): | |
29 return any((isinstance(li, list) for li in list_)) | |
30 | |
31 if not has_array(flatten): | |
32 return flatten | |
33 else: | |
34 items = [] | |
35 for item in flatten: | |
36 if isinstance(item, list): | |
37 items.extend(flatten_array(item)) | |
38 else: | |
39 items.extend(item) | |
40 return items |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment