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(seco
#coding: utf-8 """ this programe is to clear driverlog below this dir __author__:the_new_one """ import os, traceback #查找文件名中包含关键词的文件 def search_dir(s, path=os.path.abspath('.'),files = []): try: for x in os.listdir(path): pa
#!/usr/bin/python dict={"a":"apple","b":"banana","o":"orange"} print "##########dict######################" for i in dict: print "dict[%s]=" % i,dict[i] print "##########
dict={"a":"apple","b":"banana","o":"orange"} print "##########dict######################" for i in dict: print "dict[%s]=" % i,dict[i] print "###########items############
前置知识 for 循环详解:https://www.cnblogs.com/poloyy/p/15087053.html 使用 for key in dict 遍历字典 可以使用 for key in dict 遍历字典中所有的键 x = {'a': 'A', 'b': 'B'} for key in x: print(key) # 输出结果 a b 使用 for key in dict.keys () 遍历字典的键 字典提供了 keys () 方法返回字典中所有的键 # keys book =
在遍历list,删除符合条件的数据时,总是报异常,代码如下: num_list = [1, 2, 3, 4, 5] print(num_list) for i in range(len(num_list)): if num_list[i] == 2: num_list.pop(i) else: print(num_list[i]) print(num_list) 会报异常:IndexError: list index out of range 原因是在删除list中的元素后,list的实际长度变
#遍历字典, 分别打印key, value, key:value emp = {'name':'Tom', 'age':20, 'salary' : 8800.00} for k in emp.keys(): print('key = {}'.format(k)) for v in emp.values(): print('values = {}'.format(v)) for v,k in emp.items(): print('{v}:{k}'.format(v = v, k = k)) 打
如下代码,遍历列表,删除列表中的偶数时,结果与预期不符. a = [11, 20, 4, 5, 16, 28] for i in a: if i % 2 == 0: a.remove(i) print a 得到的结果为: >>> [11, 4, 5, 28] 其中偶数4和28都没有删掉,原因在于for循环在遍历列表时,是按照元素的索引依次访问元素的,当删除其中一个元素后,后面的元素会依次前移,即就是删除索引1处的元素20后,将访问索引为2的元素,但由于删除元素20之后,后面的元素会依次前