代码之家  ›  专栏  ›  技术社区  ›  CDNthe2nd

python-如果有可用性,那么打印出来,否则不要打印出来。

  •  -1
  • CDNthe2nd  · 技术社区  · 6 年前

    items['ids']

    [
      {
        'id': '11891',
        'availability': 'IsNotThere',
      },
      {
        'id': '11892',
        'availability': 'IsThere',
      },
      {
        'id': '11893',
        'availability': 'IsThere',
      },
      {
        'id': '11894',
        'availability': 'IsNotThere',
      },
      {
        'id': '11895',
        'availability': 'IsNotThere',
      },
      {
        'id': '11896',
        'availability': 'IsNotThere',
      },
      {
        'id': '11897',
        'availability': 'IsNotThere',
      },
      {
        'id': '11898',
        'availability': 'IsNotThere',
      },
      {
        'id': '11899',
        'availability': 'IsNotThere',
      },
      {
        'id': '11900',
        'availability': 'IsNotThere',
      }
    ]
    

    id_list = [i for i in products['skus'] if i.get("id")]
    
    for i in id_list:
       if i['availability'] == 'IsThere':
           print(i)
    
    2 回复  |  直到 6 年前
        1
  •  1
  •   Frank AK    6 年前

    如果你只是关心 id 你可以用它的名字取 身份证件 . 你可以这样做:

    id_list = [i for i in sample if i.get("availability") == 'IsThere']
    

    要打印出来,只需简单的循环并打印出来。

    for i in id_list:
        print(i)
    
        2
  •  1
  •   m.rp    6 年前

    如果您有一个JSON对象,您可以循环它,并使用字段名作为索引检查字段:

    for line in json:
        if(line['availability'] == "IsThere"):
           # if available -> print the id
           print(line['id'))