Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # делает список плоским с помощью рекурсии
- def flatten(curr_item, output):
- if isinstance(curr_item, list):
- for item in curr_item:
- flatten(item, output)
- else:
- output.append(curr_item)
- def main():
- a = [1, 'a', [2, 'b', [3, 'd', [[5, 'e'], [6, 'f', []]]]]]
- x = 5
- output = []
- # поиск в начальном списке
- if x in a:
- print(a[a.index(x) + 1])
- # если не найдено - делаем список плоским
- else:
- flatten(a, output)
- # поиск в плоском списке
- if x in output:
- print(output[output.index(x) + 1])
- else:
- print('В списке нет элемента: {}'.format(x))
- if __name__ == '__main__':
- main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement