python培训之判断键在不在字典里

发布时间:2022-11-08 09:47:12 人气:237 作者:多测师

  python3 中采用 in 方法

  #判断字典中某个键是否存在

  arr = {"int":"整数","float":"浮点","str":"字符串","list":"列表","tuple":"元组","dict":"字典","set":"集合"}

  #使用 in 方法

  if "int" in arr:

  print("存在")

  if "float" in arr.keys():

  print("存在")

python培训之判断键在不在字典里

  #判断键不存在

  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("存在")

  以上内容为大家介绍了python培训之判断键在不在字典里,希望对大家有所帮助,如果想要了解更多Python相关知识,请关注多测师。https://www.e70w.com/xwzx/


返回列表
在线客服
联系方式

热线电话

17727591462

上班时间

周一到周五

二维码
线