Python列表嵌套字典的时候,如果要删除列表中其中一个字典要如何操作

如果要删除列表中其中一个字典要操作如下

第一种方法

1.1

name_list=[{"name":"张三","age":18},{"name":"李四","age":22},{"name":"王五","age":26}]
name="王五"
name= input("请输入你想删除的名字:\t")
names=[item for item in name_list if not item["name"] ==name]
print(names)

1.2

def item_name():
    list = [{
        "propertyId": 3424,
        "name": "尺码",
        "value": "36",
        "propertyValueId": 20206750,
        "level": 2,
        "customValue": "",
        "showValue": 1,
        "sort": 0,
        "definitionId": 6
    }, {
        "propertyId": 4664,
        "name": "版本",
        "value": "D宽",
        "propertyValueId": 20206749,
        "level": 1,
        "customValue": "",
        "showValue": 1,
        "sort": 1,
        "definitionId": 12
    }, {
        "propertyId": 3424,
        "name": "尺码",
        "value": "37",
        "propertyValueId": 20206751,
        "level": 2,
        "customValue": "",
        "showValue": 1,
        "sort": 1,
        "definitionId": 6
    }]
    name = "版本" or "D宽"
    item_list = [item for item in list if not item["name"] == name or item["value"] == name]
    print("item_list", item_list)
    items = []
    for sale in item_list:
        sizename = sale.get("name")
        size = sale.get("value")
        ValueId = sale.get("propertyValueId")
        propertyId = sale.get("propertyId")

        sku_info = {
            "sizename": sizename,
            "size": size,
            "ValueId": ValueId,
            "propertyId": propertyId,
        }
        items.append(sku_info)
    
    print(items)
    
if __name__ == '__main__':
    item_name()

第二种方法

def item_name():
    list = [{
        "propertyId": 3424,
        "name": "尺码",
        "value": "36",
        "propertyValueId": 20206750,
        "level": 2,
        "customValue": "",
        "showValue": 1,
        "sort": 0,
        "definitionId": 6
    }, {
        "propertyId": 4664,
        "name": "版本",
        "value": "D宽",
        "propertyValueId": 20206749,
        "level": 1,
        "customValue": "",
        "showValue": 1,
        "sort": 1,
        "definitionId": 12
    }, {
        "propertyId": 3424,
        "name": "尺码",
        "value": "37",
        "propertyValueId": 20206751,
        "level": 2,
        "customValue": "",
        "showValue": 1,
        "sort": 1,
        "definitionId": 6
    }]

    # name = "版本" or "D宽"
    # item_list = [item for item in list if not item["name"] == name or item["value"] == name]
    # print("item_list", item_list)
    items = []
    for sale in list:
        size = sale.get("value")
        sizename = sale.get("name")
        if "版本" == sizename:
            continue
        # sizename = sale.get("name")
        # size = sale.get("value")
        ValueId = sale.get("propertyValueId")
        propertyId = sale.get("propertyId")

        sku_info = {
            "sizename": sizename,
            "size": size,
            "ValueId": ValueId,
            "propertyId": propertyId,
        }
        items.append(sku_info)

    print(items)


if __name__ == '__main__':
    item_name()
posted @ 2020-09-23 10:45  莫贞俊晗  阅读(4168)  评论(0编辑  收藏  举报