首页 > 编程语言 >python字典扁平化

python字典扁平化

时间:2022-10-12 08:33:59浏览次数:55  
标签:__ flat target map python prefix dict 字典 扁平化

'''
source={'a':{'b':1,'c':2},'d':{'e':3,'f':{'g':4}}}
target={}
def fun(sdict,dictkey=''):
for k,v in sdict.items():
if isinstance(v,dict):
fun(v,dictkey=dictkey+k+'.')
else:
target[dictkey+k]=v
fun(source)
print(target)
# {'a.b': 1, 'a.c': 2, 'd.e': 3, 'd.f.g': 4}

知识点
1.isinstance():判断v的类型是不是字典,如果不是就直接写入新字典;
2.递归(函数循环)
3.嵌套函数
'''
'''
sourt={'a':{'b':1,'c':2},'d':{'e':3,'f':{'g':4}},'h':5}

dict_=dict()
def flatmap(sourt,prefix=''):
for i,j in sourt.items():
if isinstance(j,dict):
flatmap(j,prefix+i+'.')
else:
dict_[prefix+i]=j
return dict_
print(flatmap(sourt))

'''
'''
1.利用递归函数处理
2.每次判断k,v键值对的value,如果value为字典,那么继续递归处理,并且对新得到的key进行改造,
追加到末尾。如果value为整数,则停止递归。在新的字典上加上新的k,v对
3.最后返回新形成的字典

'''

'''
sourt={'a':{'b':1,'c':2},'d':{'e':3,'f':{'g':4}},'h':5}

def flatmap(dis):
prefix=""
dict_={}
def innc(dis,prefix):
for i,j in dis.items():
if isinstance(j,dict):
innc(j,prefix+i+'.')
else:
dict_[prefix+i]=j
innc(dis,prefix)
return dict_
print(flatmap(sourt))

'''
'''

def flat_map(src,target=None,prefix=""):
if target is None:
target={}
for k,v in src.items():
if type(v) is dict:
flat_map(v,target,prefix+k+'.')
else:
target[prefix+k]=v
return target
if __name__=="__main__":
dict1={"a":{"b":{"c":4},"e":{"f":{"g":6}}}}
result_dict=flat_map(dict1)
print(result_dict)
'''

def flat_map_plus(src):
def _flat_map_plus(src, target=None,prefix=""):
if target is None:
target = {}
for k, v in src.items():
if type(v) is dict:
_flat_map_plus(v, target, prefix+k+".")
else:
target[prefix+k] = v
return target
target = _flat_map_plus(src)
return target
if __name__ == "__main__":
dict1 = {"a": {"b": {"c": 4}}, "e": {"f": {"g": 0}}}
new_result = flat_map_plus(dict1)
print(new_result)

标签:__,flat,target,map,python,prefix,dict,字典,扁平化
From: https://www.cnblogs.com/mengdie1978/p/16783240.html

相关文章

  • python写平台学到的东西
    1、env_dict.keys(): 获取字段所有的键,比对字符是否有里面的键 env_key=9env_dict={"1":"dev","2":"test","3":"staging"}ifenv_keynotinenv_dict.keys()......
  • python(闭包函数与装饰器)
    今日内容概要global与nonlocal函数名的多种用法闭包函数装饰器简洁介无参装饰器装饰器模板装饰器语法糖及修复技术global和nonlocalmoney=666......
  • 【GIS开发】osgEarth依赖库PROJ(Python)
    文章目录​​1、OSGeo/PROJ(C++)​​​​1.1编译sqlite3​​​​1.2编译libtiff​​​​1.3编译openssl​​​​1.4编译curl​​​​1.5编译PROJ9​​​​2、pyproj(pytho......
  • Python 多进程 multiprocessing 使用示例
    multiprocessing文档:​​https://docs.python.org/zh-cn/3.10/library/multiprocessing.html​​​Process、Lock、Semaphore、Queue、Pipe、Pool:​​https://cuiqingcai.......
  • 【机器学习】Python常见用法汇总
    【机器学习】Python常见用法汇总作者简介:在校大学生一枚,华为云享专家,阿里云星级博主,腾云先锋(TDP)成员,云曦智划项目总负责人,全国高等学校计算机教学与产业实践资源建设专家委......
  • Python 远程部署利器 Fabric2 模块
    fabric 官网英文文档:​​http://www.fabfile.org/​​《Python自动化运维技术与最佳实践》如何用Fabric实现无密码输入提示的远程自动部署:fabric实现远程操作和部署:简介F......
  • 一文了解 Python 中的对象比较方法 is 和 == 及其本质
    1Python中的对象ID我们在学习基础的时候没听说Python有C或C++中的指针啊,Python中指针是什么?先把指针这个概念放一放,一提到指针可能初学C和C++的人都害怕(本人......
  • python装饰器初级
    global与nonlocal1.global的作用:可以在局部空间里直接就该全局名称工具中的数据代码展示:name='moon'#设置了一个全局变量deffucn():name='god'#......
  • (Python)email 邮件发送
    """1.发送邮件的几个步骤:1)与邮件服务器建立会话连接2)指定用户的登录3)发送邮件2.一个标准邮件包含:1)邮件头:标题;收件人、发送人、抄送cc、密送bcc......
  • 【python】ERA5逐小时降水数据计算逐日降水
    自用简单方法参考代码:ERA5:Howtocalculatedailytotalprecipitation-CopernicusKnowledgeBase-ECMWFConfluenceWiki1、先从官网(ERA5-Landhour......