【问题标题】:Sorting instances of an Object by Numeric values Python按数值Python对对象的实例进行排序
【发布时间】:2018-02-24 09:44:54
【问题描述】:

我正在为某些值(例如名称、价格、库存和评级)抓取网站,并且一直在使用这些功能

    my_products.sort(key = lambda x: x.return_name())
    my_products.sort(key = lambda x: x.return_price())
    my_products.sort(key = lambda x: x.return_rating())
    my_products.sort(key = lambda x: x.return_stock())

对它们进行排序。这一切都很好,花花公子,除了它没有对价格进行数字排序,它将它们列为 1000、1500、20、200、2000、2500。这并不是我想要的。如何更改这种排序行为?

每个产品的类别:

class Product():
__title = ""
__price = ""
__rating =""
__stock = ""

def __init__(self,title, price, rating, stock):
    self.__title = title
    self.__price = price
    self.__rating = rating
    self.__stock = stock

def toString(self):
    return "Title: {}\nPrice: {}\nRating: {}\nStock: {}\n".format(  self.__title,
                                                                    self.__price,
                                                                    self.__rating,
                                                                    self.__stock)

def return_price(self):
    return self.__price

def return_name(self):
    return self.__title

def return_rating(self):
    return self.__rating

def return_stock(self):
    return self.__stock

【问题讨论】:

    标签: python python-3.x sorting object web-scraping


    【解决方案1】:

    当你在课堂上写它时,price 是一个字符串,那么所有的价格都会像字符串一样进行比较。 你有两个选择:

    1. 将价格转换为排序函数:my_products.sort(key = lambda x: float(x.return_price())).
      请注意,您可以通过int()float() 进行转换
    2. 将价格直接声明为数字(并且不要更改排序语法):

      class Product():
          __title = ""
          __price = np.nan
          __rating =""
          __stock = ""
      
          def __init__(self,title, price, rating, stock):
              self.__title = title
              #---
              self.__price = price#if price is a numeric input
              #or convert to the right type if price is a string input
              self.__price = float(price)
              #---
              self.__rating = rating
              self.__stock = stock
      

    在我看来,我会使用选项 2 来与价值观的含义保持一致。请注意,您应该根据新类型重写您的函数。
    也许你可以对你的评级/股票做同样的事情(如果它也是数字的话)。

    编辑 - 字符串比较和排序
    要比较字符串 python 将字符转换为它们的等效序号,然后从左到右检查 int 。所以'1000'低于'20',见下文:

    print('1000 : %d' %sum(ord(i) for i in '1000'))
    print('1500 : %d' %sum(ord(i) for i in '1500'))
    print('20 : %d' %sum(ord(i) for i in '20'))
    
    #output
    1000 : 193
    1500 : 198
    20 : 98
    

    【讨论】:

    • 好吧,我不知道排序适用于数据类型及其价值。谢谢!
    • 不客气。对于字符串比较,Python 将字符转换为其等效的序数值并从左到右比较整数(请参阅我的帖子中的更新)。
    【解决方案2】:

    您可以通过将price 设为数值而不是强制您当前拥有的字典排序的字符串来更改此行为。通常应该是:

    __price = 0
    ...
    def __init__(self, title, price, rating, stock):
        self.__price = float(price)
    

    OTOH,您可以强制用户为价格传递一个数值,而不是在您的课堂上进行清理,方法是为作为 price 传递的非数值提高 ValueError

    【讨论】:

      【解决方案3】:

      只需在您的按键函数中将字符串转换为数字(例如intfloat):

      my_products.sort(key = lambda x: float(x.return_price()))
      

      您还可以更新该方法,使其首先返回一个数字:

      def return_price(self):
          return float(self.__price)
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2012-12-24
        • 1970-01-01
        • 1970-01-01
        • 2011-11-16
        • 2023-04-07
        • 1970-01-01
        • 2010-11-02
        相关资源
        最近更新 更多