从列表中删除项目时遇到问题

时间:2016-05-04 20:01:32

标签: python function

我无法使用此功能。它的目的是从列表中删除项目。

def sell(inventory_list):
    print()
    count = int(input('How many items would you like to sell? '))
    print()

    for count in range(count):
        print()
        type = input('Enter the type of item you wish to sell? ')
        print()
        name = input('Enter the name of the item you wish to sell? ')
        print()
        price = float(input('What is the price of the item you wish to sell? $'))

        items = Plant.SubPlant(type, name, price)

        inventory_list.remove(items)

    return inventory_list

1 个答案:

答案 0 :(得分:3)

您的广告资源列表中没有您要删除的新实例。仅仅因为它包含相同的atrrs /值并不意味着它们是相同的。

为了能够这样做,可能在SubPlant类中实现方法__eq__

class SubPlant(object):
    def __init__(self, type, name, price):
        self.type = type
        self.name = name
        self.price = price

    def __eq__(self, other):
        return self.__dict__ == other.__dict__
相关问题