我爱占星网 我爱占星网
首页
编程
java
php
前端
首页 编程 java php 前端

检查给定的键是否已经存在于字典中

I wanted to test if a key exists in a dictionary before updating the value for the key. I wrote the following code:

if 'key1' in dict.keys():
  print "blah"
else:
  print "boo"

I think this is not the best way to accomplish this task. Is there a better way to test for a key in the dictionary?

转载于:https://stackoverflow.com/questions/1602934/check-if-a-given-key-already-exists-in-a-dictionary

近期文章

  • 在函数中使用全局变量
  • 如何在 Python 中获取当前时间
  • Understanding Python's slice notation
  • 在一行中捕获多个异常(块除外)
  • 如何通过引用传递变量?
  • 如何在 Python 中制作时间延迟?
  • 使用 for 循环遍历字典
  • 如何做一个功能装饰链?
  • 使用 init ()方法了解 Python super ()
  • 为什么当循环遍历整个8192元素时,我的程序会变慢?
  • 在 c + + 11中'typedef'和'using'之间有什么区别?
  • 修剪 std: : string 的最佳方法是什么?
  • 为什么要在宏中使用看似毫无意义的 do-while 和 if-else 语句?
  • 如何将 std: : string 转换为小写?
  • G + + 和 gcc 之间的区别是什么?
  • 如何在 c + + 中声明一个接口?
  • 在 c + + 中,我可以从另一个构造函数(执行构造函数链接)调用构造函数吗?
  • 如何将 std: : string 转换为 const char * 或 char * ?
  • 字符串 VS 字符串
  • 测试考拉兹猜想的 c + + 代码比手工编写的程序集更快——为什么?

Copyright ©2022 我爱占星 All Rights Reserved.

浙ICP备2022030071号-1

部分图文来自网络,如有侵犯您的版权,请告诉我们删除

友情链接:代码精华