zoukankan      html  css  js  c++  java
  • 基于可迭代数据生成一维字典

    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"))
    

      

  • 相关阅读:
    C语言中 单引号与双引号的区别
    Linux主分区,扩展分区,逻辑分区的联系和区别
    fdisk
    df du 的区别
    filesystem
    git clone
    curl
    HDR 高动态范围图像
    source ~/.bashrc 什么意思
    linux 挂载
  • 原文地址:https://www.cnblogs.com/navysummer/p/15723642.html
Copyright © 2011-2022 走看看