V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
推荐学习书目
Learn Python the Hard Way
Python Sites
PyPI - Python Package Index
http://diveintopython.org/toc/index.html
Pocoo
值得关注的项目
PyPy
Celery
Jinja2
Read the Docs
gevent
pyenv
virtualenv
Stackless Python
Beautiful Soup
结巴中文分词
Green Unicorn
Sentry
Shovel
Pyflakes
pytest
Python 编程
pep8 Checker
Styles
PEP 8
Google Python Style Guide
Code Style from The Hitchhiker's Guide
coolair
V2EX  ›  Python

用 Python 如何优雅的将 user 附加到 dept 上去?

  •  
  •   coolair · 152 天前 · 803 次点击
    这是一个创建于 152 天前的主题,其中的信息可能已经有所发展或是发生改变。

    dept 结构:

    [
        {
            "id": "2",
            "name": "aaa",
            "children": [
                {
                    "id": "3",
                    "name": "bbb",
                    "children": [
                        {
                            "id": "4",
                            "name": "ccc",
                            "children": [
                            ]
                        },
                        {
                            "id": "5",
                            "name": "ddd",
                            "children": [
                            ]
                        }
                    ]
                },
                {
                    "id": "6",
                    "name": "eee",
                    "children": [
                    ]
                }
            ]
        },
        {
            "id": "7",
            "name": "fff",
            "children": [
            ]
        }
    ]
    

    user 数据结构:

    [
        {
            "id": 2,
            "name": "aaa",
            "department": 6
        },
        {
            "id": 1,
            "name": "admin",
            "department": 7
        }
    ]
    

    合并后的结构:

    [
        {
            "id": "2",
            "name": "aaa",
            "children": [
                {
                    "id": "3",
                    "name": "bbb",
                    "children": [
                        {
                            "id": "4",
                            "name": "ccc",
                            "children": [
                            ]
                        },
                        {
                            "id": "5",
                            "name": "ddd",
                            "children": [
                            ]
                        }
                    ]
                },
                {
                    "id": "6",
                    "name": "eee",
                    "children": [
                    ],
                    "users": [
                        {
                            "id": 2,
                            "name": "aaa"
                        },
                    ]
                }
            ]
        },
        {
            "id": "7",
            "name": "fff",
            "children": [
            ],
            "users": [
                {
                    "id": 1,
                    "name": "admin"
                },
            ]
        }
    ]
    

    我目前的做法是遍历一遍 dept 列表,找到了一个 dept 就遍历以便 user 列表,如果用户多的话,这样就很慢了…… 有什么更好的办法吗?

    3 条回复    2021-06-29 10:56:55 +08:00
    dayeye2006199
        1
    dayeye2006199   152 天前   ❤️ 1
    弄个 dept -> user 的 map,是不是就快一些了
    tomkliyes
        2
    tomkliyes   152 天前
    先遍历一遍 user 得到{dept: users},再遍历 dept
    lanshee
        3
    lanshee   151 天前
    时间空间二选一,想要时间快就 map,空间小就 list.
    关于   ·   帮助文档   ·   API   ·   FAQ   ·   我们的愿景   ·   广告投放   ·   感谢   ·   实用小工具   ·   2355 人在线   最高记录 5497   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 28ms · UTC 14:01 · PVG 22:01 · LAX 06:01 · JFK 09:01
    ♥ Do have faith in what you're doing.