【问题标题】:Search for a value in a python class在 python 类中搜索一个值
【发布时间】:2018-04-16 19:44:59
【问题描述】:

我有一个客户列表(目前只有三个),每个客户都有一个唯一的代码。我希望能够使用收到的输入来搜索客户;地址、名字或姓氏。如果多个值存储在最后一个例如两个 kims - 都应该归还。我在下面包含了我的代码,但过程完成且没有返回。谁能告诉我我的代码哪里出了问题并指出我正确的方向?为什么它不接受输入、搜索列表并返回一个值?请看我的代码:

all_customers = []

class Customer:
    customers = []

    def __init__(self, first, last, address):
        self.first = first
        self.last = last
        self.address = address

    @staticmethod
    def search_members():
        print()
        user_input = input("What customer are you searching for? ")
        print()

        for i, customer in enumerate (all_customer):
            if user_input in all_customer:
                print("Test", customers)


customer_1 = Customer('Kim', 'N', 'London')
customer_2 = Customer('Chris', 'E', 'Japan')
customer_3 = Customer('Gill' 'T' 'London')

【问题讨论】:

  • 您有两个列表,而您的客户不在其中。为什么会这样?
  • all_customerall_customers?请注意,您的列表是空的,这是错误的......

标签: python list class search


【解决方案1】:

您正在尝试将用户输入字符串与客户对象进行比较。相反,您需要检查用户输入是否在客户字段之一中。下面的result 列表将包含与用户输入的名字、姓氏或地址完全匹配的所有客户。

class Customer:
    all_customers = []

    def __init__(self, first, last, address):
        self.first = first
        self.last = last
        self.address = address
        Customer.all_customers.append(self)

    @property
    def full_name(self):
        return f'{self.first} {self.last}'

    @staticmethod
    def search_members():
        print()
        user_input = input("What customer are you searching for? ")
        print()

        result = [c for c in Customer.all_customers
                  if user_input in (c.first, c.last, c.address)]

        print(f"Matching customers: {', '.join(c.full_name for c in result)}")


customer_1 = Customer('Kim', 'N', 'London')
customer_2 = Customer('Chris', 'E', 'Japan')
customer_3 = Customer('Gill', 'T', 'London')

【讨论】:

    【解决方案2】:

    您可以直接覆盖__eq__ 方法,这样如果两个客户共享一个字段,则他们比较相等。 list__contains__ 方法使用相等来判断它是否包含一个元素:

    class Customer:
    
        def __init__(self, first, last, address):
            self.first = first
            self.last = last
            self.address = address
    
        def __eq__(self, other):
            return self.first == other.first or self.last == other.last or self.address == other.address
    
    customer_1 = Customer('Kim', 'N', 'London')
    customer_2 = Customer('Chris', 'E', 'Japan')
    customer_3 = Customer('Gill', 'T', 'London') # fixed the commas
    
    customers = [customer_1, customer_2, customer_3]
    Customer('Kim', 'G', 'Berlin') in customers
    # True
    

    另一种方法是有一个类来保存您的客户并覆盖其__contains__ 方法:

    class Customers(list):
        def __contains__(self, x):
            return any(x.first == y.first or x.last == y.last or x.address == y.address for y in self)
    
    customers = Customers([customer_1, customer_2, customer_3])
    Customer('Kim', 'G', 'Berlin') in customers
    # True
    

    后者可能是首选,因为让不同的客户比较相等可能有点危险。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-12-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多