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

2024-09-30 22:28:40 发布

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

我在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”:[])

当我尝试用一个随机值替换model的值时,它工作得很好,但是我不能删除整个对象。你知道吗


Tags: 对象inurlformodeliftype单词