在python字典中没有值 [英] None value in python dictionary

查看:487
本文介绍了在python字典中没有值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

可以检查dict中没有值

Is it possible to check none value in dict

dict = {'a':'None','b':'12345','c':'None'}

我的代码

for k,v in d.items():
  if d[k] != None:
    print "good"
  else:
    print "Bad 

执行上述代码片段后打印三个好。

Prints three good after executing above code snippet.

good
good
good

必需:如果值为None,则不能为dict键a和c打印。

Required:If value is None than not printing good for dict key a and c.

推荐答案

您的任何值都不是字典中的字符串。

Your none values are actually strings in your dictionary.

您可以检查'无'
或使用实际的python 值。

d = {'a':None,'b':'12345','c':None}

for k,v in d.items(): 
  if d[k] is None:
    print "good" 
  else: 
    print "Bad"

打印好2次

或者如果您必须使用您当前的字典,只需更换支票即可查找'无'

Or if you Have to use your current dictionary just change your check to look for 'None'

另外dict是一个内置类型的python,所以这是一个不错的选择,不要命名变量 dict

additionally dict is a python built in type so it is a good idea not to name variables dict

这篇关于在python字典中没有值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆