无需线性扫描即可从json数据获取id

2024-09-18 15:14:29 发布

您现在位置:Python中文网/ 问答频道 /正文

我有以下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, 
    "image_path": "", 
    "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, 
    "image_path": "", 
    "on_discount": false, 
    "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.", 
    "discount_price": 2, 
    "id": 4, 
    "image_path": "", 
    "on_discount": false, 
    "price": 20, 
    "title": "This Is A Course About Visual Studio"
  }, 
  {
    "date_created": "2020-07-04 01:02:49", 
    "date_updated": "2021-01-31 15:07:20", 
    "description": "Scala is a multi-paradigm, general-purpose programming language.", 
    "discount_price": 3, 
    "id": 5, 
    "image_path": "", 
    "on_discount": true, 
    "price": 30, 
    "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)

在没有线性扫描的情况下,我如何做到这一点。就像有大量数据一样,最后一个变量需要花费大量的时间。所以,我希望所有变量的访问时间都相同


Tags: 数据pathimageidfalsedatetitleis
2条回答

为什么不用熊猫

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!

现在您只需通过iddf[df.id =="id"]进行选择即可

使用json包:

mydict = json.loads(myjson)

outdict = [x for x in mydict if x['id'] == id]

相关问题 更多 >