Python 的 list 怎么匹配 json ?

2022-03-29 17:14:37 +08:00
 toyst

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 = [犯罪, 悬疑, 惊悚]

应该怎么写啊

2659 次点击
所在节点    Python
11 条回复
zenxds
2022-03-29 17:23:22 +08:00
[x['name'] for x in genre_movie['genres'] if x['id'] in genre_ids]
yin1999
2022-03-29 17:24:30 +08:00
用 json 包解析一下,然后 for in 、map 随你挑
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
2022-03-29 17:39:06 +08:00
em70
2022-03-29 18:11:14 +08:00
遍历+判断,存起来输出
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
2022-03-30 09:38:28 +08:00
jsonpath
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
2022-03-30 17:57:05 +08:00
可以用正则表达式匹配,比如 /"name:""([^"]*)"/,有几个 id 就 for in range(len(xx))逐次输入到 list
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
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)

多看了一遍才发现是这样的题目,上面那个文不对题了。

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/843667

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX