如果Dict值为null

Rah*_*til 4 python dictionary

In [54]: User
Out[54]: {0: {'uid': ' rpatil\n'}, 1: {}}

In [55]: User[0]
Out[55]: {'uid': ' rpatil\n'}

In [56]: User[1]
Out[56]: {}

In [57]: if User[1] == '':
   ....:     print 'Null'
   ....:

In [58]:
Run Code Online (Sandbox Code Playgroud)

那么如何在多字典中检查Value是Null

我试过以下链接但不工作 python字典中没有值

Suk*_*lra 9

空字典是假的.只是用if not dic.

>>> testDict = {}
>>> if not testDict:
        print "Empty"


Empty
Run Code Online (Sandbox Code Playgroud)

所以,在你的代码中.

>>> User = {0: {'uid': ' rpatil\n'}, 1: {}}
>>> if not User[1]:
        print "NULL"


NULL
Run Code Online (Sandbox Code Playgroud)

  • 我不推荐这个,因为还有很多其他的东西都是假的. (3认同)

RyP*_*eck 5

空字典== {}.您还可以测试它是否为空字典.

>>> t = {}
>>> t == None
False
>>> t == {}
True
Run Code Online (Sandbox Code Playgroud)

用你的代码.

>>> User = {0: {'uid': ' rpatil\n'}, 1: {}}
>>> if User[1] == {}:
        print "NULL"

NULL
Run Code Online (Sandbox Code Playgroud)