您如何检查变量是否是python中的字典?
例如,我希望它遍历字典中的值,直到找到字典为止。然后,遍历找到的内容:
dict = {'abc': 'abc', 'def': {'ghi': 'ghi', 'jkl': 'jkl'}} for k, v in dict.iteritems(): if ###check if v is a dictionary: for k, v in v.iteritems(): print(k, ' ', v) else: print(k, ' ', v)
如果使用子类,则可以使用if type(ele) is dict或可以使用的方法:isinstance(ele, dict)dict
if type(ele) is dict
isinstance(ele, dict)
dict
d = {'abc':'abc','def':{'ghi':'ghi','jkl':'jkl'}} for ele in d.values(): if isinstance(ele,dict): for k, v in ele.items(): print(k,' ',v)