Если вы ищете оптимизацию и немного веселья ... Рекурсия - это хорошо, но не забывайте об RecursionError.
Ввод:
from datetime import datetime
from typing import Sequence
def timer(f):
def wrapped(s):
start = datetime.now()
result = f(s)
print(datetime.now() - start)
return result
return wrapped
@timer
def str_method(s: Sequence):
return [int(x) for x in
str(s).replace("(", "[").replace(")", "]")[1:-1].replace("[", "").replace("]", "")
if x not in [",", " "]]
def flatten(s: Sequence):
if not s:
return s
if isinstance(s[0], (list, tuple)):
return list(flatten(s[0])) + list(flatten(s[1:]))
return list(s[:1]) + list(flatten(s[1:]))
if __name__ == '__main__':
s = [1, 2, (3, 4, (5, 6))]
start = datetime.now()
print(flatten(s))
print(datetime.now() - start)
print(str_method(s))
print("-")
s = [(x, ) for x in range(100)]
start = datetime.now()
flatten(s)
print(datetime.now() - start)
str_method(s)
print("-")
s = [(x, ) for x in range(100000)]
start = datetime.now()
try:
print(flatten(s))
except RecursionError:
print("RecursionError")
str_method(s)
Вывод:
[1, 2, 3, 4, 5, 6]
0:00:00.000022 # flatten
0:00:00.000041 # str_method
[1, 2, 3, 4, 5, 6]
-
0:00:00.000369 # flatten
0:00:00.000122 # str_method
-
RecursionError # flatten
0:00:00.186894 # str_method