Issue
This is my code:
for current, next in zip(xlist, ylist):
start = datetime.strptime(current["id"], '%Y-%m-%d').date()
next_start = datetime.strptime(next["id"], '%Y-%m-%d').date()
I am wondering if there is a way to use lambda for writing start
and next_start
in one line of code. I have tried the following code however it is not correct:
for current, next in zip(xlist, ylist):
start, next_start = lambda X:datetime.strptime(X, '%Y-%m-%d').date()
Solution
Why would you want to declare them in one line? They are two separate dates requiring two different variables.
You can set a lambda function as:
set_date = lambda x: datetime.strptime(x, "%Y-%m-%d").date()
start = set_date(current)
but next_start requires a completely different variable to set the date.
Also, start, next_start = lambda X:datetime.strptime(X, '%Y-%m-%d').date()
This syntax is causing start & next_start to have the same value.
To me, it looks like we would have to go out of our way to force "Pythonic" patterns, and turn what is seemingly an easy solution, into something unreadable.
Answered By - alex067
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.