您的位置:首页 > 编程语言 > Python开发

python isinstance方法 嵌套字典类型数据 输出最里层value

2017-06-02 09:25 836 查看
转自:http://www.jb51.net/article/96469.htm

isinstance方法判断一个对象是否属于某类,isinstance(object, classinfo) 

例如:isinstance(1, int) 返回True

isinstance(a,dict)  判断对象a是否为字典,如果为真,会打印True,如为假,打印False。

应用示例:

def list_all_dict(dict_a): 

 

 if isinstance(dict_a,dict) : #使用isinstance检测数据类型 

 

  for x in dict_a: 

 

   #temp_key = dict_a.keys()[x] 

 

   temp_value = dict_a[x] 

 

   #print ("%s %s"%(x, temp_value)) 

 

   list_all_dict(temp_value) #自我调用实现无限遍历

 else:

   print (dict_a)

person = {"male":{"name":"Shawn"}, "female":{"name":"Betty","age":23},"children":{"name":{"first_name":"李", "last_name":{"old":"明明","now":"铭"}},"age":4}}

list_all_dict(person)
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: