a={'a':{'b':{'c':{'d':'e'}},'f':'g'},'h':'i'}
def show(myMap):
for str in myMap.keys():
secondDict=myMap[str]
print str
if type(myMap[str]).__name__=='dict':
for key in secondDict.keys():
if type(secondDict[key]).__name__=='dict':
print key
show(secondDict[key])
else:
print key
print secondDict[key]
else:
print myMap[str]
show(a)
通过递归来实现遍历字典中的所有元素。效率可能比较低。
Python遍历字典
1.遍历key值 1 >>> d = {'Python':'astonishing', 'C++':'complicated', 'Java':'versatile'} 2 > ...
python检查字典元素是否存在类似php中isset()方法
PHP中isset()方法来检查数组元素是否存在,在Python中无对应函数,在Python中一般可以通过异常来处理数组元素不存在的情况,而无须事先检查 Python的编程理念是“包容错误”而不是“严 ...
Python遍历字典dict的几种方法