Python代码阅读第47篇从列表右

北京儿童医院痤疮 http://baidianfeng.39.net/a_wh/210612/9056770.html

Python代码阅读合集介绍:为什么不推荐Python初学者直接看项目源码

#python#

本篇阅读的代码实现了将给定的函数倒序依次应用在列表元素上的功能。

本篇阅读的代码片段来自于30-seconds-of-python。

for_each_right

deffor_each_right(itr,fn):forelinitr[::-1]:fn(el)#EXAMPLESfor_each_right([1,2,3],print)#

for_each_right函数接收一个迭代列表和一个应用函数,返回将函数从右到左应用函数的结果。

函数使用for循环和列表切片itr[::-1]倒序遍历列表,在for循环体内,对每个列表元素应用函数fn。




转载请注明:http://www.aierlanlan.com/rzfs/2481.html