小编Dir*_*ann的帖子

Python - 从元组列表中生成字典(树)

我有以下列表: -

a = [(1, 1), (2, 1), (3, 1), (4, 3), (5, 3), (6, 3), (7, 7), (8, 7), (9, 7)]
Run Code Online (Sandbox Code Playgroud)

这是一个元组列表.元组内的元素的格式是(Id, ParentId)其根节点Id == ParentId.列表可以是元组的任何顺序.

我想使用上面的元组列表生成以下字典,

output = [{
    'id': 1,
    'children': [{
        {
            'id': 3,
            'children': [{
                {
                    'id': 5
                },
                {
                    'id': 4
                },
                {
                    'id': 6
                }
            }]
        },
        {
            'id': 2
        }
    }]
}, {
    'id': 7,
    'children': [{
        {
            'id': 9
        },
        {
            'id': 8
        }
    }]
}]
Run Code Online (Sandbox Code Playgroud)

即(就图表而言 - …

python tree

9
推荐指数
1
解决办法
1万
查看次数

python 中是否有相当于 emacs 的 rx 宏?

emacs 中的 rx 宏(参见http://www.emacswiki.org/emacs/rxhttp://doc.endlessparentheses.com/Fun/rx)使得以模块化和可读的方式指定正则表达式成为可能(位于至少你不必关心引用问题)。例如:

(rx "a" (optional "c") "b")
Run Code Online (Sandbox Code Playgroud)

结果是

"ac?b"
Run Code Online (Sandbox Code Playgroud)

python中有类似的东西吗?

python regex emacs

5
推荐指数
1
解决办法
256
查看次数

标签 统计

python ×2

emacs ×1

regex ×1

tree ×1