我不明白这个语法。在Python program to demonstrate ternary operatora, b = 10, 20Use tuple for selecting an item
^{pr2}$
Use Dictionary for selecting an itemprint({True: a, False: b} [a < b])
PS:我想这是来自旧版本的Python,因为在较新的版本中(不知道是哪个版本的)True和False是保留关键字,所以不能给它们赋值。在lamda is more efficient than above two methods
because in lambda we are assure that only one expression will be evaluated unlike in
tuple and Dictionaryprint((lambda: b, lambda: a)[a < b]())
语法应该是:[on_true] if [expression] else [on_false]
那怎么办print( (b, a) [a < b] )
print({True: a, False: b} [a < b])
print((lambda: b, lambda: a)[a < b]())
适合这个语法吗?
tuple/dictionary/lambda后面的[a
我希望它看起来像这样print( a if a < b else b )