一个灵活的实用程序,用于在python中展平和取消展平dict-like对象。

flatten-dict的Python项目详细描述


请访问GitHub repository 欲了解更多信息。

扁平dict

https://img.shields.io/travis/ianlini/flatten-dict/master.svghttps://img.shields.io/pypi/v/flatten-dict.svghttps://img.shields.io/pypi/l/flatten-dict.svg

一个灵活的实用程序,用于在Python中展开和不展开类似DITT的对象。

简介

这个python包提供了一个函数flatten(),用于展平dict类对象。 它还提供了一些关键的连接方法(reducer),您可以选择所需的reducer,甚至可以实现自己的reducer。您也可以选择反转生成的平面dict。

文件

扁平化

defflatten(d,reducer='tuple',inverse=False,enumerate_types=()):"""Flatten `Mapping` object.

    Parameters
    ----------
    d : dict-like object
        The dict that will be flattened.
    reducer : {'tuple', 'path', Callable}
        The key joining method. If a `Callable` is given, the `Callable` will be
        used to reduce.
        'tuple': The resulting key will be tuple of the original keys.
        'path': Use `os.path.join` to join keys.
    inverse : bool
        Whether you want invert the resulting key and value.
    enumerate_types : Sequence[type]
        Flatten these types using `enumerate`.
        For example, if we set `enumerate_types` to ``(list,)``,
        `list` indices become keys: ``{'a': ['b', 'c']}`` -> ``{('a', 0): 'b', ('a', 1): 'c'}``.

    Returns
    -------
    flat_dict : dict
    """
示例
In[1]:fromflatten_dictimportflattenIn[2]:normal_dict={...:'a':'0',...:'b':{...:'a':'1.0',...:'b':'1.1',...:},...:'c':{...:'a':'2.0',...:'b':{...:'a':'2.1.0',...:'b':'2.1.1',...:},...:},...:}In[3]:flatten(normal_dict)Out[3]:{('a',):'0',('b','a'):'1.0',('b','b'):'1.1',('c','a'):'2.0',('c','b','a'):'2.1.0',('c','b','b'):'2.1.1'}In[4]:flatten(normal_dict,reducer='path')Out[4]:{'a':'0','b/a':'1.0','b/b':'1.1','c/a':'2.0','c/b/a':'2.1.0','c/b/b':'2.1.1'}In[5]:flatten(normal_dict,reducer='path',inverse=True)Out[5]:{'0':'a','1.0':'b/a','1.1':'b/b','2.0':'c/a','2.1.0':'c/b/a','2.1.1':'c/b/b'}In[6]:defunderscore_reducer(k1,k2):...:ifk1isNone:...:returnk2...:else:...:returnk1+"_"+k2...:In[7]:flatten(normal_dict,reducer=underscore_reducer)Out[7]:{'a':'0','b_a':'1.0','b_b':'1.1','c_a':'2.0','c_b_a':'2.1.0','c_b_b':'2.1.1'}

如果我们有一些iterable(例如,list)在dict中,我们通常会得到这个:

In[8]:flatten({'a':[1,2,3],'b':'c'})Out[8]:{('a',):[1,2,3],('b',):'c'}

如果要将其索引用作键,则可以使用参数枚举类型

In[9]:flatten({'a':[1,2,3],'b':'c'},enumerate_types=(list,))Out[9]:{('a',0):1,('a',1):2,('a',2):3,('b',):'c'}

我们甚至可以直接展开列表

In[10]:flatten([1,2,3],enumerate_types=(list,))Out[10]:{(0,):1,(1,):2,(2,):3}

不平坦
defunflatten(d,splitter='tuple',inverse=False):"""Unflatten dict-like object.

    Parameters
    ----------
    d : dict-like object
        The dict that will be unflattened.
    splitter : {'tuple', 'path', Callable}
        The key splitting method. If a Callable is given, the Callable will be
        used to split.
        'tuple': Use each element in the tuple key as the key of the unflattened dict.
        'path': Use `pathlib.Path.parts` to split keys.
    inverse : bool
        Whether you want to invert the key and value before flattening.

    Returns
    -------
    unflattened_dict : dict
    """
示例
In[1]:fromflatten_dictimportunflattenIn[2]:flat_dict={...:('a',):'0',...:('b','a'):'1.0',...:('b','b'):'1.1',...:('c','a'):'2.0',...:('c','b','a'):'2.1.0',...:('c','b','b'):'2.1.1',...:}In[3]:unflatten(flat_dict)Out[3]:{'a':'0','b':{'a':'1.0','b':'1.1'},'c':{'a':'2.0','b':{'a':'2.1.0','b':'2.1.1'}}}In[4]:flat_dict={...:'a':'0',...:'b/a':'1.0',...:'b/b':'1.1',...:'c/a':'2.0',...:'c/b/a':'2.1.0',...:'c/b/b':'2.1.1',...:}In[5]:unflatten(flat_dict,splitter='path')Out[5]:{'a':'0','b':{'a':'1.0','b':'1.1'},'c':{'a':'2.0','b':{'a':'2.1.0','b':'2.1.1'}}}In[6]:flat_dict={...:'0':'a',...:'1.0':'b/a',...:'1.1':'b/b',...:'2.0':'c/a',...:'2.1.0':'c/b/a',...:'2.1.1':'c/b/b',...:}In[7]:unflatten(flat_dict,splitter='path',inverse=True)Out[7]:{'a':'0','b':{'a':'1.0','b':'1.1'},'c':{'a':'2.0','b':{'a':'2.1.0','b':'2.1.1'}}}In[8]:defunderscore_splitter(flat_key):...:returnflat_key.split("_")...:In[9]:flat_dict={...:'a':'0',...:'b_a':'1.0',...:'b_b':'1.1',...:'c_a':'2.0',...:'c_b_a':'2.1.0',...:'c_b_b':'2.1.1',...:}In[10]:unflatten(flat_dict,splitter=underscore_splitter)Out[10]:{'a':'0','b':{'a':'1.0','b':'1.1'},'c':{'a':'2.0','b':{'a':'2.1.0','b':'2.1.1'}}}

欢迎加入QQ群-->: 979659372 Python中文网_新手群

推荐PyPI第三方库


热门话题
java用变化的替换字符串替换子字符串   从数据库中断中恢复的oracle Java DAL?   Android/Java页边距位于左/右/底部   java如何用相同的源代码构建不同的APK?(我发现了一个错误)   java正则表达式,仅当字符串以一行中的3个数字开头时才匹配第一个数字   使用以xml为输入的给定端点调用java中的rest-ful web服务?   java长字符串转换为UTF8引发异常   java如何使用截取方法获取ArrayList   java将计算列添加到可观察列表中   正则表达式如何在java正则表达式中使用组?   java正则表达式只接受字母表和空格,不允许在字符串的开头和结尾使用空格   java简单onclick按钮在安卓中不起作用   java如何在Spring中只实现Crudepository的特定方法?   java无法使用json对象NPE读取jsonarray   java我可以添加maven依赖项,这些依赖项被打包为除此之外的任何东西。罐子