Python字典遇到的value中遇到None跳出问题

2025-05-09 18:01:41
推荐回答(2个)
回答1:

#没有什么特别的算法,循环加上判断就可以了。。。
hosts = {"a":"111", "b":None, "c":"333"}
hname = ""
for (k,v) in hosts.items():
    if v == None:
        print "key: %s, value= " % (k)
    else:
        print "key: %s, value = %s" % (k, v)
#key: a, value = 111
#key: c, value = 333
#key: b, value=

回答2:

d={'clab129node09': [['was', 'ALL', '7277', u'NFS'], ['was', 'ALL', '637', u'NFS']],'clab129node08': None, 'clab129node03': [[u'FM', 'ALL', '2049']]……}
for i in d:
if d[i]==None:
print "this key's value equal None"
else:
print d[i]#这个是一个列表(看样子应该是2维的,要获取里面的值也很简单,通过索引即可,获取全部的值只需用for就可以)