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 = [犯罪, 悬疑, 惊悚]
应该怎么写啊
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]
|
2
yin1999 2022-03-29 17:24:30 +08:00 via Android
用 json 包解析一下,然后 for in 、map 随你挑
|
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) |
4
jsutfun 2022-03-29 17:39:06 +08:00
<script src="https://gist.github.com/hlpureboy/637f657d70f9b349c6a1ac1748173dc1.js"></script>
|
5
em70 2022-03-29 18:11:14 +08:00
遍历+判断,存起来输出
|
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() |
7
hhhhhh123 2022-03-30 09:38:28 +08:00
jsonpath
|
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) |
9
ALLROBOT 2022-03-30 17:57:05 +08:00
可以用正则表达式匹配,比如 /"name:""([^"]*)"/,有几个 id 就 for in range(len(xx))逐次输入到 list
|
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) |
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) 多看了一遍才发现是这样的题目,上面那个文不对题了。 |