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 · 2021-06-28 15:09:15 +08:00 · 1362 次点击
    这是一个创建于 1246 天前的主题,其中的信息可能已经有所发展或是发生改变。

    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  
       2021-06-28 15:39:13 +08:00   ❤️ 1
    弄个 dept -> user 的 map,是不是就快一些了
    tomkliyes
        2
    tomkliyes  
       2021-06-28 15:53:14 +08:00
    先遍历一遍 user 得到{dept: users},再遍历 dept
    lanshee
        3
    lanshee  
       2021-06-29 10:56:55 +08:00
    时间空间二选一,想要时间快就 map,空间小就 list.
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   实用小工具   ·   1023 人在线   最高记录 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 24ms · UTC 22:01 · PVG 06:01 · LAX 14:01 · JFK 17:01
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.