, :
, .
Reduce will apply a function of two arguments cumulatively to the items of a sequence,
from left to right, so as to reduce the sequence to a single value.
:
reduce(function, sequence[, initial]) -> value
, ,
, .
:
>>> reduce(lambda x, y: x+y, [1, 2, 3, 4, 5])
15
>>>
smiliar to ((((1+2)+3)+4)+5)
:
>>> reduce(lambda x, y: x+y, [], 1)
1
>>>
, :
, dict.get():
>>> d = {'a': {'b': {'c': 'files'}}}
>>> dict.get(d,'a')
{'b': {'c': 'files'}}
>>>
, dict.get reduce, :
>>> d = {'a': {'b': {'c': 'files'}}}
{'b': {'c': 'files'}}
>>> reduce(dict.get, ['a','b','c'], d)
'files'
>>>
:
>>> dict.get(dict.get(dict.get(d,'a'),'b'),'c')
'files'
>>>
, dict, :
>>> reduce(dict.get, [], {})
{}
>>>
:
dir snippet!= dir(), , .
parent = reduce(dict.get, folders[:-1], dir)
, [: - 1] - . dir is empty_dictionary.
, , .