【问题标题】:How can I conduct a search, using a partial string, to search a list of objects with string parameters and find the object I'm looking for in Python?如何使用部分字符串进行搜索以搜索具有字符串参数的对象列表并在 Python 中找到我要查找的对象?
【发布时间】:2020-05-02 21:24:53
【问题描述】:

我正在创建的是一种商店。我可以使用多个参数将产品对象添加到所述商店(也是一个对象),包括标题(字符串)、描述(字符串)和产品编号(整数)。

我想创建一个函数,该函数将一个字符串作为参数,该字符串可用于搜索库存(列表)中所有产品对象的标题和描述参数,查找匹配项(部分和全部,它没有在这种情况下很重要),将这些匹配的对象附加到一个名为 search_results 的列表中,按照它们的 product_number 升序对它们进行排序,然后将它们返回给用户。

    class Product:

        def __init__(self, code, title, desc, price, quantity):
            self._code = code
            self._title = title
            self._desc = desc
            self._price = price
            self._quantity = quantity

        def decrease_quantity(self):
            self._quantity = self._quantity - 1

        def get_product_code(self):
            return self._code

        def get_product_title(self):
            return self._title

        def get_product_price(self):
            return self._price

        def get_product_quantity(self):
            return self._quantity

        def get_product_desc(self):
            return self._desc

    class Store:

        def __init__(self):
            self._store_inventory = []
            self._store_members = []

        def product_search(self, product_keyword):
            search_results = []
            for product in self._store_inventory:
                if product_keyword in product.get_product_title():
                    search_results.append(product)
                elif product_keyword in product.get_product_desc():
                    search_results.append(product)
                else:
                    return "Not Found"
            search_results.sort()
            return search_results

这是我目前所拥有的。

我遇到的主要问题(我知道有几个)是我无法让 product_search 函数正常工作。也就是说,它总是恢复“else”大小写并返回“Not Found”,即使我给出了产品标题的确切大小写和拼写。

在这方面我做错了什么?

【问题讨论】:

  • 省略Product() 的代码使得无法知道像product.get_product_title() 这样的方法会返回什么方法,这使得无法解释为什么像product_keyword in product.get_product_title() 这样的比较不能按预期工作。当人们不得不猜测您的代码时,主要会出现混淆。
  • @Grismar 道歉。已编辑。
  • 这会有所帮助,但现在的问题是,当您创建 titledesc 时,您使用的值与 desc 完全相同 - 而不是来回来回,也许遵循以下步骤: stackoverflow.com/help/minimal-reproducible-example

标签: python string list search


【解决方案1】:

您可能需要根据docsproduct 类实现函数__eq__

对于 list、tuple、set、frozenset、dict 或 collections.deque 等容器类型,表达式 x in y 等价于 any(x is e or x == e for e in y)

in 运算符不起作用的原因是该类的 __eq__ 函数未实现,因此 Python 不知道如何比较您的对象实例(要比较哪些属性......)

阅读更多关于__eq__

例子:

class Foo:
    def __init__(self, a):
        self.a = a

class Bar:
    def __init__(self, a):
        self.a = a

    def __eq__(self, other):
        return self.a == other.a
f1 = Foo(5)
f2 = Foo(5)
print("f1 == f2", f1 == f2)
fl = [f1,f2]
f3 = Foo(5)
print("f3 in fl", f3 in fl)

g1 = Bar(5)
g2 = Bar(5)
print("g1 == g2",g1 == g2)
gl = [g1,g2]
g3 = Bar(5)
print("g3 in gl", g3 in gl)

输出:

f1 == f2 False
f3 in fl False
g1 == g2 True
g3 in gl True

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-04-27
    • 2016-03-15
    • 1970-01-01
    • 1970-01-01
    • 2023-03-24
    • 1970-01-01
    • 2016-04-14
    相关资源
    最近更新 更多