Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Need to convert to lambda function

i try to write this function to lambda function,I tried a lot of options and I could not success:

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

I know to begin with:

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

thanks! in to example if: s=(1,(2,(3,4))) then getitem_rlist(a,2))# -> 3 the function need to return the element at index i of recursive list s

like image 958
user2976686 Avatar asked Nov 10 '13 17:11

user2976686


People also ask

Why do we need Lambda functions?

Why Use Lambda Functions? Lambda functions are used when you need a function for a short period of time. This is commonly used when you want to pass a function as an argument to higher-order functions, that is, functions that take other functions as their arguments.

What can I use instead of lambda in Python?

One of good alternatives of lambda function is list comprehension. For map() , filter() and reduce() , all of them can be done using list comprehension. List comprehension is a solution between a regular for-loop and lambda function.

Which can be used instead of lambda expression?

How to replace lambda expression with method reference in Java 8. If you are using a lambda expression as an anonymous function but not doing anything with the argument passed, you can replace lambda expression with method reference.


1 Answers

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

maybe what you want .... Its hard to tell withpout knowing what those other methods do ...

>>> 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
like image 62
Joran Beasley Avatar answered Oct 06 '22 08:10

Joran Beasley