【问题标题】:Class attribute - Delete a Dictionary from a List类属性 - 从列表中删除字典
【发布时间】:2020-09-23 21:24:46
【问题描述】:

我正在尝试为用户编写代码,以便能够删除之前添加的条目。当前列表如下所示:

[{"_Item__id": 1,"item_name": "MAC PowerBook", "item_type": "Computer", "date_add": "01/06/2020", "dom": "16/02/2000", "item_info": "description of product"}, 
{"_Item__id": 2,"item_name": "Nokia 6210", "item_type": "Phone", "date_add": "01/06/2020", "dom": "01/02/2000", "item_info": "description of product"},  
{"_Item__id": 3, "item_name": "Kodak Retina", "item_type": "Camera", "date_add": "04/06/2020", "dom": "20/12/1931", "item_info": "description of product"}]

尝试使用以下代码:

def delete_item():
    print("{0:3}\t{1:20}\t{2:10}\t{3:10}".format("ID", "Item", "Date added", "Date manufactured"))
    for i in Item.py_collection_list:
        print("{0:03d}\t{1:20}\t{2:10}\t{3:10}".format(i.get_id(), i.item_name, i.date_add, i.dom))
        remove_item = input('Type name of the item you would like to delete from collection> ')
        if remove_item == "item_name":
            Item.py_collection_list[{"item_name"}].remove()

然后被重定向到主菜单。

试过这段代码:

def delete_item():
    print("{0:3}\t{1:20}\t{2:10}\t{3:10}".format("ID", "Item", "Date added", "Date manufactured"))
    for i in Item.py_collection_list:
        print("{0:03d}\t{1:20}\t{2:10}\t{3:10}".format(i.get_id(), i.item_name, i.date_add, i.dom))
        remove_item = input('Type name of the item you would like to delete from collection> ')
        if remove_item == i.item_name:
            for item in Item.py_collection_list[:]:
                if item['remove_item'] <= 0:
                    Item.py_collection_list.remove(item)

得到了这个错误:

TypeError: 'Item' object is not subscriptable

试过这段代码:

def delete_item():
    print("{0:3}\t{1:20}\t{2:10}\t{3:10}".format("ID", "Item", "Date added", "Date manufactured"))
    for i in Item.py_collection_list:
        print("{0:03d}\t{1:20}\t{2:10}\t{3:10}".format(i.get_id(), i.item_name, i.date_add, i.dom))
        remove_item = input('Type name of the item you would like to delete from collection> ')
        if remove_item == i.item_name:
            Item.py_collection_list.remove(remove_item)

得到了这个错误:

Item.py_collection_list.remove(remove_item)

ValueError: list.remove(x): x 不在列表中

试过这段代码:

def delete_item():
    print("{0:3}\t{1:20}\t{2:10}\t{3:10}".format("ID", "Item", "Date added", "Date manufactured"))
    for i in Item.py_collection_list:
        print("{0:03d}\t{1:20}\t{2:10}\t{3:10}".format(i.get_id(), i.item_name, i.date_add, i.dom))
        remove_item = input('Type name of the item you would like to delete from collection> ')
        for i in range(len(Item.py_collection_list)):
            if Item.py_collection_list[i]["item_name"] == remove_item:
                del Item.py_collection_list[i]

得到了这个错误:

if Item.py_collection_list[i]["item_name"] == remove_item:

TypeError: 'Item' 对象不可下标

试过这段代码:

def delete_item():
    print("{0:3}\t{1:20}\t{2:10}\t{3:10}".format("ID", "Item", "Date added", "Date manufactured"))
    for i in Item.py_collection_list:
        print("{0:03d}\t{1:20}\t{2:10}\t{3:10}".format(i.get_id(), i.item_name, i.date_add, i.dom))
        remove_item = input('Type name of the item you would like to delete from collection> ')
        if remove_item in Item.py_collection_list:
            del Item.py_collection_list[i.item_name]
            return True
        return False

被重定向到主菜单

这似乎是一个非常直接的创建选项,但我似乎完全在监督这个选项。

【问题讨论】:

  • 什么是Item? “被重定向到主菜单”是什么意思?什么主菜单?
  • Item 是一个类,列表是一个变量 - toptal.com/python/… 。上面的代码不会出错,只是让我回到我正在尝试构建的程序的主菜单。

标签: python-3.x list dictionary class-attributes


【解决方案1】:

我建议先单独打印字典,即,如果您想向用户提供可用项目的列表,然后再要求输入以删除项目。

接下来你可以像这样在循环外输入。

remove_item = input("Type name of the item you would like to delete from collection:")
for i in Item.py_collection_list:
   if remove_item == i.item_name:
      Item.py_collection_list.remove(i)

这会从列表中删除与该项目对应的对象。

如果您想删除项目对象的“item_name”属性,请使用以下内容。

remove_item = input("Type name of the item you would like to delete from collection:")
for i in Item.py_collection_list:
   if remove_item == i.item_name:
      delattr(i, "item_name")

【讨论】:

  • 感谢您抽出宝贵时间对此进行调查。不幸的是,我似乎收到了这个错误: if remove_item == i['item_name']: TypeError: 'Item' object is not subscriptable
  • 你写的这个delete_item函数,是类模块吗?
  • 这个链接解释了如何正确访问类变量toptal.com/python/…
  • 是 Item 是一个类,py_collection_list 是存储数据的列表,如文章所述: class Item: py_collection_list = [] def __init__(self, item_name, item_type, date_add, dom, item_info) : self.__id = Item.get_next_id() self.item_name = item_name self.item_type = item_type self.date_add = date_add self.dom = dom self.item_info = item_info Item.py_collection_list.append(self)
  • 查看此代码,“Item.py_collection_list”不是项目字典列表。但是类对象的列表,因为您将“self”附加到列表中。您必须通过 for 循环中的“i.item_name”访问它。我已经更新了答案。
猜你喜欢
  • 2020-09-28
  • 2020-07-14
  • 2010-11-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-11-27
相关资源
最近更新 更多