python - 需要转换为 lambda 函数

标签 python python-3.x

我尝试将此函数写入 lambda 函数,我尝试了很多选项但都没有成功:

 def getitem_rlist(s, i):
    while i > 0:    
      s, i = rest(s), i - 1
    return first(s)

我知道开始时:

getitem_rlist=lambda s,i:....?

谢谢! 例如,如果:s=(1,(2,(3,4))) 然后 getitem_rlist(a,2))# -> 3 该函数需要返回递归列表 s 的索引 i 处的元素

最佳答案

getitem_rlist=lambda s,i: getitem_rlist(s[1:][0],i-1) if i > 0 else s[0]

也许是你想要的......如果不知道其他方法的作用,很难说......

>>> getitem_rlist=lambda s,i: getitem_rlist(s[1:][0],i-1) if i > 0 else s[0]
>>> s=(1,(2,(3,4)))
>>> getitem_rlist(s,2)
3

关于python - 需要转换为 lambda 函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19892792/

相关文章:

python - 无法使用正则表达式获得自定义结果

Python-OpenCV 查找矩形并裁剪图像

python - 什么是最优雅的 Pythonic 方法来删​​除 CSV 中的两个连续记录并保留一个记录?

python - 如果索引和列相同,如何在单独的列中获取数据集的相关值

python - 具有多个参数的 R 扫描的高效 python pandas 等效/实现

python - 无法阅读在线提供的 pdf 文件的特定页面的内容

python - xlsxwriter 在满行之间创建空行

python - 看似随机的 ssl.SSLError : [SSL: TLSV1_ALERT_INTERNAL_ERROR] tlsv1 alert internal error

python - 如何在Python中增量计算罚款?

python-3.x - python 3.2 : IOError: [Errno 22] Invalid argument: '/home/pi/data/temp/file1\n.txt'