字典的每个键值(key=>value)对用冒号(:)分割,每个对之间用逗号(,)分割,整个字典包括在花括号({})中 ,格式如下所示:
d = {key1 : value1, key2 : value2 }
键必须是唯一的,但值则不必。
值可以取任何数据类型,但键必须是不可变的,如字符串,数字或元组。
python3 中采用 in 方法
#判断字典中某个键是否存在 arr = {"int":"整数","float":"浮点","str":"字符串","list":"列表","tuple":"元组","dict":"字典","set":"集合"} #使用 in 方法 if "int" in arr: print("存在") if "float" in arr.keys(): print("存在") #判断键不存在 if "floats" not in arr: print("不存在") if "floats" not in arr: print("不存在")
python 3中不支持 has_key(),python2 中支持
#判断字典中某个键是否存在 arr = {"int":"整数","float":"浮点","str":"字符串","list":"列表","tuple":"元组","dict":"字典","set":"集合"} #使用自带的has_key() if(arr.has_key("int")): print("存在")
Copyright © 2019- howto234.com 版权所有 湘ICP备2022005869号-3
违法及侵权请联系:TEL:199 1889 7713 E-MAIL:2724546146@qq.com
本站由北京市万商天勤律师事务所王兴未律师提供法律服务