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
css3
V2EX  ›  Python

如何在 dict 内,根据指定的 value,查找出想要的 key 和 value

  •  
  •   css3 · 2022-02-08 11:12:20 +08:00 · 3614 次点击
    这是一个创建于 1053 天前的主题,其中的信息可能已经有所发展或是发生改变。

    请教一下,python内给定以下dict, 已知"Huber"= "Tucker", 求"Huber"= "Tucker"所在dict内的data_id及其在"Armstrong"内的索引

    {
        "Armstrong": [
            {
                "date_id": "1",
                "show_name": "Quynn",
                "description": "Quynn",
                "commid": "Ward",
                "Mooney": {
                    "Carpenter": "Bolton",
                    "Amena": "Elijah",
                    "Rice": {
                        "Kitra": false,
                        "Huber": "Shafira",
                        "novalidate": "Valdez0"                    
                    }
                },
                "Graham": "Mariko",
                "status": {
                    "status": "OFF",
                },
                "Alyssa": {
                    "Little": "Noelani",
                    "device_id": "Sparks",
                },
                "Judah": "Huff"
            },
            {
                "date_id": "2",
                "show_name": "Holland",
                "description": "Holland",
                "commid": "Spencer",
                "Mooney": {
                    "Carpenter": "Illana",
                    "Amena": "Rosa",
                    "Rice": {
                        "Kitra": false,
                        "Huber": "Tucker",
                        "novalidate": "Evelyn"                    
                    }
                },
                "Graham": "Janna",
                "status": {
                    "status": "ON",
                },
                "Alyssa": {
                    "Little": "Drew",
                    "device_id": "Herman",
                },
                "Judah": "England"
            }
            # ....很多
        ],
        "page": {
            "offset": 0,
            "limit": 100,
            "total": 2
        }
    }
    
    10 条回复    2022-02-08 18:57:40 +08:00
    wannaspring
        1
    wannaspring  
       2022-02-08 11:26:47 +08:00
    def fun (targetv, dicts):
    if isinstance(dicts, dict):
    for key,value in d.items():
    if value == targetv:
    return key
    if isinstance(value, dict):
    fun(targetv, value)
    if isinstance(dicts, list):
    wannaspring
        2
    wannaspring  
       2022-02-08 11:27:45 +08:00
    不是很熟悉 python ,但是这样是不是可以?,剩下的 list 自己补一下
    NessajCN
        3
    NessajCN  
       2022-02-08 11:56:41 +08:00
    for r in dict["Armstrong"]:
    if r["Mooney"]["Rice"]["Huber"]=="Tucker":
    print r["date_id"]
    print dict["Armstrong"].index(r)
    ykk
        4
    ykk  
       2022-02-08 13:43:17 +08:00
    字典是 O(n) 遍历就完了,已知某些规律可以缩小遍历空间
    seven123
        5
    seven123  
       2022-02-08 14:08:39 +08:00
    遍历就完了
    julyclyde
        6
    julyclyde  
       2022-02-08 15:49:06 +08:00
    这是不是传说中的“倒排索引”?
    wsgfz000
        7
    wsgfz000  
       2022-02-08 16:17:17 +08:00
    试试 bidict
    Latin
        8
    Latin  
       2022-02-08 17:36:10 +08:00
    SenLief
        9
    SenLief  
       2022-02-08 18:41:43 +08:00
    这不是遍历吗
    wellsc
        10
    wellsc  
       2022-02-08 18:57:40 +08:00 via iPhone
    倒排索引
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   实用小工具   ·   1021 人在线   最高记录 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 20ms · UTC 19:26 · PVG 03:26 · LAX 11:26 · JFK 14:26
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.