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

时间:2019-06-13 15:26:46

标签: 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的值时,效果很好,但是我无法删除整个对象。

1 个答案:

答案 0 :(得分:0)

遍历results的副本:

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