Python 003- 小知识汇总(更新中)

#查询key是否存在,可以在使用未知的字典的时候使用

 1 #-*- coding:utf-8 -*-
 2 D={'a':1,'c':3,'b':2}
 3 for key in sorted(D):
 4     print(key,'->',D[key])
 5 
 6 if not D.has_key('f'):
 7     print('this key is not in dic')
 8 
 9 
10 #lambda表达式,可以理解为一个未定义的函数
11 lambda_plus=lambda x:x+1
12 print(lambda_plus(1))
原文地址:https://www.cnblogs.com/hustcser/p/8830235.html