Skip to content

Instantly share code, notes, and snippets.

@ImageAsInput
Last active March 15, 2017 13:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ImageAsInput/e0a05239e39241dac7827e10b8d03a23 to your computer and use it in GitHub Desktop.
Save ImageAsInput/e0a05239e39241dac7827e10b8d03a23 to your computer and use it in GitHub Desktop.
#Input Variable
nestedX = [[[[[1, 2, [[[3]]]], 4]]]]
#Recursive function written quickly.
def flatten(nX):
flatX = []
for x in nX:
if isinstance(x,int):
flatX += [x]
else:
flatX += flatten(x)
return flatX
#print the output
print flatten(nestedX)
#Funny and ugly way to do it :P :P (list function needed for python 3.x not 2.x). Never do it in REAL LIFE!
#print list(map(int,str(nestedX).replace('[','').replace(']','').split(',')))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment