本文共 5129 字,大约阅读时间需要 17 分钟。
my_dict = { "name":"alien", "country":"china", "other":{ "age":"18", "gender":"man", }, "school":"HLW"}(1)获取字典里面的key,vaule,遍历每个元祖def read_dict_all(dict_str): for i in dict_str.items(): # items()函数可以获取对象的key,value print(i)read_dict_all(my_dict)(2)获取字典里面的key,vaule,都是单独的字符串def read_dict_only(dict_str): for k,v in dict_str.items(): print(k,v)read_dict_only(my_dict)(3)获取字典里面的key或value,获取的是单独字符串def read_dict_only_k(dict_str): for k,v in dict_str.items(): print(k)read_dict_only_k(my_dict)
最终结果:
('name', 'alien')('country', 'china')('other', { 'age': '18', 'gender': 'man'})('school', 'HLW')name aliencountry chinaother { 'age': '18', 'gender': 'man'}school HLWnamecountryotherschool
my_dict = { "name":"alien", "country":"china", "other":{ "age":"18", "gender":"man", "family":{ "wife":"lily", "pet":"nuomi" } }, "school":"HLW"}for i in my_dict.keys(): print(i)print('+'*30)for i in my_dict.values(): print(i)
最终结果:
namecountryotherschool++++++++++++++++++++++++++++++alienchina{ 'age': '18', 'gender': 'man', 'family': { 'wife': 'lily', 'pet': 'nuomi'}}HLW
def dict_get(dict_str): for key in dict_str.keys(): return key ##for循环的时候,如果return之前可能有多个结果,那只返回第一个结果def dict_read(dict_str, key_t): for key in dict_str.keys(): if key==key_t: ## return之前,需要确定唯一的结果时候,再return结果 return dict_str.get(key_t)my_dict = { "name":"alien", "country":"china", "school":"HLW"}r = dict_get(my_dict)print(r)r =dict_read(my_dict,"school")print(r)
最终结果:
nameHLW
my_dict = { "name":"alien", "country":"china", "other":{ "age":"18", "gender":"man", "family":{ "wife":"lily", "pet":"nuomi" } }, "school":"HLW"}def read_dict(dict_str,key): for k, v in dict_str.items(): if k == key: return v if type(v) is dict: return read_dict(v,key) return "在dic()里面未发现目标key"r_pet = read_dict(my_dict, "pet") # 读取第三级字典的keyprint(r_pet)r_gender = read_dict(my_dict,'gender') # 读取第二季字典的keyprint(r_gender)r_alien = read_dict(my_dict,'alien') # 非dict里面的keyprint(r_alien)
最终结果:
nuomiman在dic()里面未发现目标key
def dict_get(dict_str, key_t): if isinstance(dict_str, dict): for v in dict_str.values(): if key_t in dict_str.keys(): return dict_str.get(key_t) else: if type(v) is dict: return dict_get(v,key_t) return '传入的字符串不是字典或未查到目标key_t'
错误解读:
def dict_get(dict_str, key_t): if isinstance(dict_str, dict): for v in dict_str.values(): if key_t in dict_str.keys(): return dict_str.get(key_t) else: return dict_get(v,key_t) return '传入的字符串不是字典或未查到目标key_t'
- 如果要找的是key是“pet”,那么在在第一个for循环的时候,从for直接跳到else,而此时的v是alien,alien不是字典
- 所以alien带入函数dict_get()的时候,会直接被return无法进入if isinstance()函数
def dict_get(dict_str, key_t): if isinstance(dict_str,dict): if key_t in dict_str.keys(): return dict_str.get(key_t) else: for v in dict_str.values(): if type(v) is dict: return dict_get(v,key_t) return '传入的字符串不是字典或未查到目标key_t'
错误解读:
def dict_get(dict_str, key_t): if isinstance(dict_str,dict): if key_t in dict_str.keys(): return dict_str.get(key_t) else: for v in dict_str.values(): return dict_get(v,key_t)
- 错误的地方与方式二一样
错误解读三:
def dict_get(dict_str, key_t): if isinstance(dict_str,dict): for key in dict_str.keys(): if key_t in dict_str.keys(): return dict_str.get(key_t) else: return dict_get(dict_str.get(key), key_t)
- return里面 dict_str.get(key)这个也不一定是字典,如果目标key是pet,那么第一个for循环的时候,alien带入return的dict_get()函数,最终肯定也会返回none,
json_str = [ { "name": "alien", "country": "china", "other": { "age": "18", "gender": "man", "family": { "wife": "lily", "pet": "nuomi" } }, "school": "清华" }, { "name": "lily", "country": "china", "other": { "age": "20", "gender": "woman", }, "school": "北大" }]def print_keyvalue_by_key(input_json, key): key_value = '' if isinstance(input_json, dict): for json_result in input_json.values(): if key in input_json.keys(): key_value = input_json.get(key) else: print_keyvalue_by_key(json_result, key) elif isinstance(input_json, list): for json_array in input_json: print_keyvalue_by_key(json_array, key) if key_value != '': print(str(key) + " = " + str(key_value))print_keyvalue_by_key(json_str,'name')print_keyvalue_by_key(json_str,"pet")
最终结果:
name = alienname = lilypet = nuomi
转载地址:http://boeab.baihongyu.com/