点击查看代码
def get_nested_value(data, keys, values):dataCopy = dataif not keys:return FalsefoundEnd = Falsefor key, value in zip(keys, values):print(key, value)subkeys = key.split('.')foundMid = Falsefor subkey in subkeys:print("subkey值{}".format(subkey))print("data值{}".format(dataCopy))found = Falseif dataCopy is None:return False# 判断是否是对象if isinstance(dataCopy, dict):dataCopy = dataCopy.get(subkey)print("值{}".format(dataCopy))# 判断是否是字符串或者数字,是的话直接比较if(isinstance(dataCopy, (int, str))):if str(dataCopy) == value:foundMid = Trueprint("单层 {}".format(foundMid))breakcontinue# 判断是否是数组,是的话遍历数组elif isinstance(dataCopy, list):if subkey.isdigit() and int(subkey) < len(dataCopy):dataCopy = dataCopy[int(subkey)]else:print("222")foundThird = Falsefor item in dataCopy:print("item值{}".format(item))if isinstance(item, dict) and subkey in item:dataCopy = item[subkey]print("值{}".format(dataCopy))if(isinstance(dataCopy, (int, str))):if str(dataCopy) == value:foundThird = Trueprint("多层{}".format(foundThird))breakprint("2221232222222")continueprint("2221232222")if isinstance(dataCopy, list):print("item值{}".format(item))print("2222222")if subkey.isdigit() and int(subkey) < len(dataCopy):dataCopy = dataCopy[int(subkey)]else:print("2222")for item in dataCopy:if isinstance(item, dict) and subkey in item:print("33333")dataCopy = item[subkey]print("值{}".format(dataCopy))if(isinstance(dataCopy, (int, str))):if str(dataCopy) == value:found = Trueprint("内多层{}".format(found))breakcontinueprint("aaaa2221232222")if not found:print("not多层{}".format(not found))breakelse:foundMid = foundif not found:print("not多层{}".format(not found))breakelse:foundMid = found# return Falseelse:print("333")if not found:print("not单层{}".format(found))else:foundMid = foundif not foundMid:print("not foundMid2 {}".format(foundMid))foundEnd = Falseelse:print("foundMid2 {}".format(foundMid))foundEnd = TruedataCopy = datareturn foundEnd