热线电话:13121318867

登录
2019-03-30 阅读量: 402
区分元组,字典和lambda的用法

使用元组,字典和lambda的直接方法

# Python program to demonstrate ternary operator

a, b = 10, 20

# Use tuple for selecting an item

print( (b, a) [a < b] )

# Use Dictionary for selecting an item

print({True: a, False: b} [a < b])

# 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 Dictionary

print((lambda: b, lambda: a)[a < b]())

输出:
10
10
10

85.7167
1
关注作者
收藏
评论(0)

发表评论

暂无数据
推荐帖子