跳转到主要内容

一组用于操作字典的有用工具。

项目描述

dictutils

Build Status

一组用于操作字典的有用工具。

dictutils.qsdict

将字典列表或对象转换为嵌套字典。

lst = [
    {"shape": "circle", "colour": "blue", "count": 5},
    {"shape": "circle", "colour": "pink", "count":15},
    {"shape": "square", "colour": "yellow", "count": 29},
    {"shape": "square", "colour": "blue", "count": 10}
]

qsdict(lst, "shape", "colour", "count")

# returns

{
    "circle": {
        "blue": 5,
        "pink": 15
    },
    "square": {
        "yellow": 29,
        "blue": 10
    }
}

qsdict(lst, "colour", "shape", "count")

# returns

{
    "blue": {
        "circle": 5,
        "square": 10
    },
    "pink": {
        "circle": 15
    },
    "yellow": {
        "square": 29
    }
}

也可以接受可调用对象

qsdict(lst, lambda x: x["colour"][0:2], "shape", "count")

{
    "bl": {
        "circle": 5,
        "square": 10
    },
    "pi": {
        "circle": 15
    },
    "ye": {
        "square": 29
    }
}

访问任意数量的参数

lst = [
    {"shape": "circle", "colour": "blue", "country": "France", "count": 5},
    {"shape": "circle", "colour": "pink", "country": "Germany", "count":15},
    {"shape": "square", "colour": "yellow", "country": "France", "count": 29},
    {"shape": "square", "colour": "blue", "country": "China", "count": 10}
]

qsdict(lst, lambda x: x["colour"][0:2], "shape", "country","count")

# Returns
{
    "bl": {
        "circle": {
            "France": 5
        },
        "square": {
            "China": 10
        }
    },
    "pi": {
        "circle": {
            "Germany": 15
        }
    },
    "ye": {
        "square": {
            "France": 29
        }
    }
}

如果您希望叶节点为列表,则将元组作为最后一个参数传递

qsdict(lst, lambda x: x["colour"][0:2], "shape", ("country","count"))

{
    "bl": {
        "circle": [
            "France",
            5
        ],
        "square": [
            "China",
            10
        ]
    },
    "pi": {
        "circle": [
            "Germany",
            15
        ]
    },
    "ye": {
        "square": [
            "France",
            29
        ]
    }
}

dictutils.mergedict

合并两个嵌套字典。请注意,第一个字典将被更新。

d1 = {
    "blue": {
        "circle": {
            "France": 5
        },
        "square": {
            "China": 10
        }
    },
    "pink": {
        "circle": {
            "Germany": 15
        }
    },
    "yellow": {
        "square": {
            "France": 29
        }
    }
}

d2 = {
    "blue": {
        "brightness": 4,
    },
    "pink": {
        "brightness": 4,
    },
    "yellow": {
        "brightness": 4,
    }
}

mergedict(d1, d2)

print(d1)

{
    "blue": {
        "circle": {
            "France": 5
        },
        "square": {
            "China": 10
        },
        "brightness": 4
    },
    "pink": {
        "circle": {
            "Germany": 15
        },
        "brightness": 4
    },
    "yellow": {
        "square": {
            "France": 29
        },
        "brightness": 4
    }
}

如果您不想覆盖第一个字典,请提供一个空字典

d0 = {}
mergedict(d0, d1)
mergedict(d0, d2)

这可以重复任意次数,以创建复杂的数据结构,同时避免嵌套循环和难以管理的代码。此代码由Stack Overflow 此线程提供。

dictutils.pivot

根据给定的键列表旋转字典

d1 = {
    "A": {
        "Category1": {
            "X": 111111,
            "Y": 222222,
        },
        "Category2": {
            "X": 333333,
            "Y": 444444,
        },
        "Category3": {
            "X": 555555,
            "Y": 666666,
        }
    },
    "B": {
        "Category1": {
            "X": 777777,
            "Y": 888888,
        },
        "Category2": {
            "X": 999999,
            "Y": 101010,
        },
        "Category3": {
            "X": 101011,
            "Y": 101012,
        }
    },
}

print(pivot(d, [2, 1, 0])

{
    "X": {
        "Category1": {
            "A": 111111,
            "B": 777777,
        },
        "Category2": {
            "A": 333333,
            "B": 999999,
        },
        "Category3": {
            "A": 555555,
            "B": 101011,
        },
    },
    "Y": {
        "Category1": {
            "A": 222222,
            "B": 888888,
        },
        "Category2": {
            "A": 444444,
            "B": 101010,
        },
        "Category3": {
            "A": 666666,
            "B": 101012,
        },
    },
}

项目详情


下载文件

下载适用于您平台的文件。如果您不确定选择哪个,请了解更多关于 安装包 的信息。

源代码分发

dictutils-0.1.7.tar.gz (8.7 kB 查看哈希值)

上传时间 源代码

由以下提供支持

AWS AWS 云计算和安全赞助商 Datadog Datadog 监控 Fastly Fastly CDN Google Google 下载分析 Microsoft Microsoft PSF 赞助商 Pingdom Pingdom 监控 Sentry Sentry 错误记录 StatusPage StatusPage 状态页面