def get_path(data, value, flag=True): """ :param data: 被查找的数据 :param value: 要查的数据 :param flag: False:模糊查找,True:精确查找 :return: """ dt = gen_dict(data) paths = [] for k, v in dt.items(): if not isinstance(v, str) or not isinstance(v, str) or flag: if v == value: paths.append(k) else: if value in v: paths.append(k) return paths def gen_dict(data): dt = dict() if data: if isinstance(data, dict): for key, value in data.items(): if isinstance(value, dict) or isinstance(value, tuple) or isinstance(value, list): nt = gen_dict(value) for k, v in nt.items(): dt[f"{key}.{k}"] = v else: dt[key] = value elif isinstance(data, tuple) or isinstance(data, list): for i, value in enumerate(data): if isinstance(value, dict) or isinstance(value, tuple) or isinstance(value, list): nt = gen_dict(value) for k, v in nt.items(): dt[f"{i}.{k}"] = v else: dt[i] = value else: dt["unknown"] = data return dt data = { "s": "5", "t": ["5", "3", "5", {"name": "5"}] } print(gen_dict(data)) print(get_path(data, "5"))