回答
像其他人一樣,我不確定你在問什麼,但我願意猜測。
我有時會使用lambda表達式來處理API調用的結果。
說例如一個API調用結果的元素應該是一個數字字符串,我希望作爲一個整數,但偶爾它會返回別的東西。
你可以定義一個lambda將一個字符串轉換成整數,如果它是由數字:
lambda x: x and x.isdigit() and int(x) or None
這避免了if
聲明,但lambda
不是因爲,你可以做同樣的功能:
def f(x):
return x and x.isdigit() and int(x) or None
更新
bug更少的黑客,保羅·麥圭爾的禮貌:
lambda x: int(x) if x and x.isdigit() else None
即作爲int('0')
返回False
等效拉姆達可以通過返回None
當你想要0
這只是在Python中沒有包含三進制運算符的舊解決方法,現代的方式是'int(x)if x and x.isdigit()else None',雖然這意味着你不想轉換'0.0'。 – agf
正如@agf指出的那樣,如果其中一個術語成功評估爲一個Python被視爲布爾型False的值,則此舊解決方法一直存在做錯事情的風險。如果你打算指示某人使用這種黑客攻擊,至少應該使用更少的漏洞攻擊:'lambda x:int(x)if x and x.isdigit()else None'。現在,當x爲'0'時,您不會感到驚訝。 – PaulMcG
如果x是「-1」,該怎麼辦? x.isdigit()也在這裏返回False。 – PaulMcG
我可能是嚴重偏離,但我想這意味着類似:
filter(lambda x: x > 0, list_of_values)
會回到你的元素從list_of_values
具有大於0
if condition:
do_stuff()
else:
dont()
值爲
(lambda x: do_stuff() if x else dont())(condition)
但目前還不清楚你在找什麼。
請注意,這是而不是和if
聲明 - 這是一個ternary operation。在Python中,它們都使用if
這個詞。在C#中查看例如Lambda "if" statement?。
也許你是指這樣的東西(Lambda calculus)?
If = lambda test, x, y: test(x, y)
True = lambda x, y: x
False = lambda x, y: y
,你可以使用像...
# I guess you have to convert them sometimes... oh well
C = lambda b: [False, True][b]
x = If(C(2 > 3), "Greater", "Less")
print(x)
# "Less"
但現在事情開始土崩瓦解......
If(C(2 > 3), print("Greater"), print("Less"))
# Invalid syntax unless you use
# from __future__ import print_function
# And if you do, it prints both!
# (Because python has eager evaluation)
# So we could do
True = lambda x, y: x()
False = lambda x, y: y()
# And then
If(C(2 > 3), lambda:print("Greater"), lambda:print("Less"))
# "Less"
所以,不那麼漂亮,還是有用的。但它的工作。
下面是一個小竅門,通過Smalltalk的 語言核心的啓發,不if語句也不三元 運營商使用,但因爲工作會讓你大吃一驚有條件執行 機制。 :-)
#!/usr/bin/env python
class ATrue:
def ifThen(self,iftrue): iftrue()
def ifThenElse(self,iftrue,iffalse): return iftrue()
def andThen(self,other): return other()
def orElse(self,other): return self
class AFalse:
def ifThen(self,iftrue): pass
def ifThenElse(self,iftrue,iffalse): return iffalse()
def andThen(self,other): return self
def orElse(self,other): return other()
def echo(x): print x
if __name__=='__main__':
T = ATrue()
F = AFalse()
x = T # True
y = T.andThen(lambda: F) # True and False
z = T.orElse(lambda: F) # True or False
x.ifThenElse(lambda: echo("x is True"), lambda: echo("x if False"))
y.ifThenElse(lambda: echo("y is True"), lambda: echo("y if False"))
z.ifThenElse(lambda: echo("z is True"), lambda: echo("z if False"))
更新:整理一些符號,以避免混淆,並明確指出。並添加了代碼以顯示如何對邏輯運算符執行快捷評估。
- 1. Switch語句而不是if語句
- 2. Lambda Consumer if語句
- 3. Lambda裏面if語句?
- 4. if/else語句,而不是切換
- 5. 的Javascript不是if語句
- 6. 我的if語句執行兩個if而不僅僅是1
- 7. Jekyll IF語句爲'IF索引(主頁)而不是分頁'?
- 8. Lambda表達式if-else語句
- 9. 包含if語句的lambda函數
- 10. JavaScript if語句要做 - 而
- 11. if語句,而切片
- 12. ASH if if語句中的if語句
- 13. PHP IF語句中的IF IF語句
- 14. if語句是不是裏面的setInterval
- 15. 我可以在此代碼中使用case語句而不是if語句嗎?
- 16. 不空if語句
- 17. IF語句不VBA
- 18. 如果語句嵌套if if語句嵌套if語句,
- 19. 切換語句而不是多重嵌套if - else?
- 20. 爲什麼Forth使用IF語句THEN ...而不是ENDIF?
- 21. 在python中使用子程序而不是if語句
- 22. 使用開關語句,而不是多個IF的
- 23. 兩個If語句一次執行,而不是一個
- 24. 如何寫數組而不是使用if語句
- 25. 使用虛函數而不是IF語句更快?
- 26. 有人可以切換到if語句而不是開關?
- 27. 在if語句中處理HttpStatusCode,而不是try/catch
- 28. 應用函數而不是嵌套循環if語句
- 29. 價值分配,而不是用在if語句
- 30. 創建一個循環,而不是重複if語句
如果您以其他語言提供了參考或示例,這將有所幫助。 – agf