Advertisement
Aikiro42

directory_diving

Feb 14th, 2020
266
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.47 KB | None | 0 0
  1. def flatten(li):
  2.     flat_li = []
  3.     for e in li:
  4.         if isinstance(e, list):
  5.             flat_li += flatten(e)
  6.         else:
  7.             flat_li.append(e)
  8.     return flat_li
  9.  
  10. def ndeep(folder, n):
  11.     files = []
  12.     if n == 0:
  13.         return folder
  14.     for file in folder:
  15.         if isinstance(file, list):
  16.             files += ndeep(file, n-1)
  17.     return flatten(files)
  18.  
  19. # folder = ['a', ['b'], ['c', [['d'], 'e']]]
  20. # n = 3
  21. # print(ndeep(folder, n))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement