在 Python 中删除 If 语句中的对象



我在JSON文件中有一个对象数组。如果一个单词不在对象内的值中,我想删除整个对象。

for x in results:
    if "Berline" in auction_type.split():
        if any(word in auction_model for word in berlinewords):
            del x
        else:
          pass
    else:
      pass

不行。

数据库示例:

"objectID": 11460,
        "type": "Coupé",
        "cars_getroute": "bugatti-type-57-stelvio-cabriolet-1934-1936",
        "gm_url": "https://www.url.com",
        "results": [
            {
                "marque": "Bugatti",
                "model": "Type 57 Stelvio",
                "model_year": "1936",
                "price_str": "Estimate $900 000 - $1 200 000 (unsold)",
                "price_int": null,
                "price_currency": null,
                "sold": false,
                "auction_house": "RM Sotheby's",
                "auction_country": "USA",
                "auction_date": "12 mars 2016",
                "auction_datetime": "2016-03-12",
                "auction_url": null,
                "image_urls": "https://www.url.com",
                "price_int_eu": null
            }
        ]
如果单词">

Berline"在"type"中,另一个在"model"中的其他单词列表中,我想删除该对象(在示例中只是"results":[](

当我只是尝试用随机模型替换模型的值时,它运行良好,但我无法删除整个对象。

遍历results的副本:

for o in results[:]:
    if <condition>:
        results.remove(o)
...

最新更新