Advertisement
Guest User

Untitled

a guest
Apr 3rd, 2020
36
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.81 KB | None | 0 0
  1. # делает список плоским с помощью рекурсии
  2. def flatten(curr_item, output):
  3.     if isinstance(curr_item, list):
  4.         for item in curr_item:
  5.             flatten(item, output)
  6.     else:
  7.         output.append(curr_item)
  8.  
  9.  
  10. def main():
  11.     a = [1, 'a', [2, 'b', [3, 'd', [[5, 'e'], [6, 'f', []]]]]]
  12.     x = 5
  13.     output = []
  14.  
  15.     # поиск в начальном списке
  16.     if x in a:
  17.         print(a[a.index(x) + 1])
  18.     # если не найдено - делаем список плоским
  19.     else:
  20.         flatten(a, output)
  21.  
  22.     # поиск в плоском списке
  23.     if x in output:
  24.         print(output[output.index(x) + 1])
  25.     else:
  26.         print('В списке нет элемента: {}'.format(x))
  27.  
  28.  
  29. if __name__ == '__main__':
  30.     main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement