Форум сайта python.su
Вот эти (map, filter, в частности reduce) функции вообще только для красоты или или от них есть практическая польза?
>>>
>>> a = []
>>> for i in range(2000):
... a.append(set(range(i)))
...
>>> def f1(a):
... s = set()
... for aa in a : s |= aa
...
>>> def f2(a):
... s = reduce(set.union, a)
...
>>> import cProfile
>>> cProfile.run('f1(a)')
3 function calls in 0.420 CPU seconds
Ordered by: standard name
ncalls tottime percall cumtime percall filename:lineno(function)
1 0.420 0.420 0.420 0.420 <stdin>:1(f1)
1 0.000 0.000 0.420 0.420 <string>:1(<module>)
1 0.000 0.000 0.000 0.000 {method 'disable' of '_lsprof.Profiler' objects}
>>> cProfile.run('f2(a)')
4 function calls in 1.150 CPU seconds
Ordered by: standard name
ncalls tottime percall cumtime percall filename:lineno(function)
1 0.000 0.000 1.150 1.150 <stdin>:1(f2)
1 0.000 0.000 1.150 1.150 <string>:1(<module>)
1 0.000 0.000 0.000 0.000 {method 'disable' of '_lsprof.Profiler' objects}
1 1.150 1.150 1.150 1.150 {reduce}
>>>
Офлайн
Ну если выполняемая бинарная функция заранее неизвестна (вместо set.union), например, то reduce - вполне подходящее короткое решение.
Офлайн
IsemХмм… Да, пожалуй.
Ну если выполняемая бинарная функция заранее неизвестна
Офлайн