如何解决无需线性扫描即可从json数据中获取id
我有以下 json 数据:
[
{
"date_created": "2019-12-25 12:57:58","date_updated": "2020-12-18 16:18:29","description": "Scala is a multi-paradigm,general-purpose programming language.","discount_price": 2,"id": 1,"image_path": "","on_discount": false,"price": 20,"title": "The Art of Scala"
},{
"date_created": "2019-03-16 05:15:39","date_updated": "2020-12-29 08:40:44","description": "Agile practices discover requirements and develop solutions through collaborative effort.","discount_price": 3,"id": 2,"on_discount": true,"price": 30,"title": "The Joy of Agile"
},{
"date_created": "2020-09-13 14:40:39","date_updated": "2020-09-23 10:52:39","description": "Pages is a word processor developed by Apple Inc.","discount_price": 5,"id": 3,"price": 50,"title": "Talks About Pages"
},{
"date_created": "2020-07-20 17:51:23","date_updated": "2020-08-04 12:06:58","description": "Microsoft Visual Studio is an integrated development environment (IDE) from Microsoft.","id": 4,"title": "This Is A Course About Visual Studio"
},{
"date_created": "2020-07-04 01:02:49","date_updated": "2021-01-31 15:07:20","id": 5,"title": "Even A Kid Can Learn Scala!"
}]
我想使用数据中的键“id”获取记录。我是这样做的:
for i in data_id:#data_id is the json objects
if i['id']==id:
print(i)
没有线性扫描我怎么能做到这一点。就好像有大量数据一样,最后一个变量需要大量时间。所以,我希望所有变量的访问时间都相同
解决方法
为什么不使用 Pandas?
df = pd.DataFrame(your_json_data)
display(df)
打印出来:
date_created date_updated description discount_price id image_path on_discount price title
0 2019-12-25 12:57:58 2020-12-18 16:18:29 Scala is a multi-paradigm,general-purpose pro... 2 1 False 20 The Art of Scala
1 2019-03-16 05:15:39 2020-12-29 08:40:44 Agile practices discover requirements and deve... 3 2 True 30 The Joy of Agile
2 2020-09-13 14:40:39 2020-09-23 10:52:39 Pages is a word processor developed by Apple Inc. 5 3 False 50 Talks About Pages
3 2020-07-20 17:51:23 2020-08-04 12:06:58 Microsoft Visual Studio is an integrated devel... 2 4 False 20 This Is A Course About Visual Studio
4 2020-07-04 01:02:49 2021-01-31 15:07:20 Scala is a multi-paradigm,general-purpose pro... 3 5 True 30 Even A Kid Can Learn Scala!
现在您只需通过 id
和 df[df.id =="id"]
进行选择。
为此使用 json 包:
mydict = json.loads(myjson)
outdict = [x for x in mydict if x['id'] == id]
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。