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

Python 的 list 怎么匹配 json ?

  •  
  •   toyst · 2022-03-29 17:14:37 +08:00 · 2664 次点击
    这是一个创建于 971 天前的主题,其中的信息可能已经有所发展或是发生改变。

    genre_movie = '{"genres":[{"id":28,"name":"动作"},{"id":12,"name":"冒险"},{"id":16,"name":"动画"},{"id":35,"name":"喜剧"},{"id":80,"name":"犯罪"},{"id":99,"name":"纪录"},{"id":18,"name":"剧情"},{"id":10751,"name":"家庭"},{"id":14,"name":"奇幻"},{"id":36,"name":"历史"},{"id":27,"name":"恐怖"},{"id":10402,"name":"音乐"},{"id":9648,"name":"悬疑"},{"id":10749,"name":"爱情"},{"id":878,"name":"科幻"},{"id":10770,"name":"电视电影"},{"id":53,"name":"惊悚"},{"id":10752,"name":"战争"},{"id":37,"name":"西部"}]}'

    genre_ids = [80, 9648, 53]

    想匹配生成 genre_ids = [犯罪, 悬疑, 惊悚]

    应该怎么写啊

    11 条回复    2022-04-17 16:54:08 +08:00
    zenxds
        1
    zenxds  
       2022-03-29 17:23:22 +08:00   ❤️ 2
    [x['name'] for x in genre_movie['genres'] if x['id'] in genre_ids]
    yin1999
        2
    yin1999  
       2022-03-29 17:24:30 +08:00 via Android
    用 json 包解析一下,然后 for in 、map 随你挑
    thinkershare
        3
    thinkershare  
       2022-03-29 17:35:29 +08:00
    如果有大量的字典类型的需要处理的, 可以使用 easydict, 最好还是将数据清洗一下, 这样效率高一点
    import json
    from easydict import EasyDict;

    genre_movie = '{"genres":[{"id":28,"name":"动作"},{"id":12,"name":"冒险"},{"id":16,"name":"动画"},{"id":35,"name":"喜剧"},{"id":80,"name":"犯罪"},{"id":99,"name":"纪录"},{"id":18,"name":"剧情"},{"id":10751,"name":"家庭"},{"id":14,"name":"奇幻"},{"id":36,"name":"历史"},{"id":27,"name":"恐怖"},{"id":10402,"name":"音乐"},{"id":9648,"name":"悬疑"},{"id":10749,"name":"爱情"},{"id":878,"name":"科幻"},{"id":10770,"name":"电视电影"},{"id":53,"name":"惊悚"},{"id":10752,"name":"战争"},{"id":37,"name":"西部"}]}'

    movies=EasyDict(json.loads(genre_movie)).genres
    genre_ids = [80, 9648, 53]
    genre_names = [movie.name for movie in movies if movie.id in genre_ids]
    print(genre_names)
    jsutfun
        4
    jsutfun  
       2022-03-29 17:39:06 +08:00
    em70
        5
    em70  
       2022-03-29 18:11:14 +08:00
    遍历+判断,存起来输出
    TimePPT
        6
    TimePPT  
       2022-03-29 19:10:55 +08:00
    import pandas as pd
    import json

    genre_movie = '{"genres":[{"id":28,"name":"动作"},{"id":12,"name":"冒险"},{"id":16,"name":"动画"},{"id":35,"name":"喜剧"},{"id":80,"name":"犯罪"},{"id":99,"name":"纪录"},{"id":18,"name":"剧情"},{"id":10751,"name":"家庭"},{"id":14,"name":"奇幻"},{"id":36,"name":"历史"},{"id":27,"name":"恐怖"},{"id":10402,"name":"音乐"},{"id":9648,"name":"悬疑"},{"id":10749,"name":"爱情"},{"id":878,"name":"科幻"},{"id":10770,"name":"电视电影"},{"id":53,"name":"惊悚"},{"id":10752,"name":"战争"},{"id":37,"name":"西部"}]}'

    genre_ids = [80, 9648, 53]

    data = json.loads(genre_movie)
    df = pd.DataFrame(data=data["genres"])

    genre_names = df[df["id"].isin(genre_ids)]["name"].to_list()
    hhhhhh123
        7
    hhhhhh123  
       2022-03-30 09:38:28 +08:00
    jsonpath
    kaiger
        8
    kaiger  
       2022-03-30 11:43:35 +08:00
    import json

    genre_movie = '{"genres":[{"id":28,"name":"动作"},{"id":12,"name":"冒险"},{"id":16,"name":"动画"},{"id":35,"name":"喜剧"},{"id":80,"name":"犯罪"},{"id":99,"name":"纪录"},{"id":18,"name":"剧情"},{"id":10751,"name":"家庭"},{"id":14,"name":"奇幻"},{"id":36,"name":"历史"},{"id":27,"name":"恐怖"},{"id":10402,"name":"音乐"},{"id":9648,"name":"悬疑"},{"id":10749,"name":"爱情"},{"id":878,"name":"科幻"},{"id":10770,"name":"电视电影"},{"id":53,"name":"惊悚"},{"id":10752,"name":"战争"},{"id":37,"name":"西部"}]}'

    genre_movie_dict = json.loads(genre_movie)
    genre_ids = [80, 9648, 53]
    genre_name = [genre.get('name') for genre in genre_movie_dict.get('genres') if genre.get('id') in genre_ids]
    print(genre_name)
    ALLROBOT
        9
    ALLROBOT  
       2022-03-30 17:57:05 +08:00
    可以用正则表达式匹配,比如 /"name:""([^"]*)"/,有几个 id 就 for in range(len(xx))逐次输入到 list
    youdoit
        10
    youdoit  
       2022-04-17 16:41:29 +08:00
    初学者献丑,看看是不是这样

    list_str = [] # 文字列表
    list_int = [] # 数字列表

    for k,v in genre_movie.items():
    for i in v: # 循环 values,这也是一个字典
    for key, values in i.items():
    if type(values) is not int: # 判断值是否为整数类型,写入相应的 list 列表
    list_str.append(values)
    else:
    list_int.append(values)

    print(list_str, list_int)
    youdoit
        11
    youdoit  
       2022-04-17 16:54:08 +08:00
    for k,v in genre_movie.items():
    for i in v: # 循环 values,这也是一个字典
    if i.get('id') in genre_ids: # 获取字典里的 id 值,并且存在列表里面
    list_str.append(i.get('name')) # 输出对于的 id 值一样的数据到列表

    print(list_str)

    多看了一遍才发现是这样的题目,上面那个文不对题了。
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   实用小工具   ·   1202 人在线   最高记录 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 24ms · UTC 17:51 · PVG 01:51 · LAX 09:51 · JFK 12:51
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.