新聞中心
這里有您想知道的互聯(lián)網(wǎng)營銷解決方案
創(chuàng)新互聯(lián)Python教程:python如何在自定義類上使用堆排序
1、說明

創(chuàng)新互聯(lián)公司為企業(yè)級客戶提高一站式互聯(lián)網(wǎng)+設(shè)計服務(wù),主要包括成都網(wǎng)站設(shè)計、成都網(wǎng)站建設(shè)、成都app軟件開發(fā)、成都小程序開發(fā)、宣傳片制作、LOGO設(shè)計等,幫助客戶快速提升營銷能力和企業(yè)形象,創(chuàng)新互聯(lián)各部門都有經(jīng)驗豐富的經(jīng)驗,可以確保每一個作品的質(zhì)量和創(chuàng)作周期,同時每年都有很多新員工加入,為我們帶來大量新的創(chuàng)意。
我們留給自定義類的唯一解決方案是實際重寫比較運算符。遺憾的是,這使我們局限于對每個類只能進行一種比較。在我們的示例中,我們被局限于按年份對Movie對象進行排序。
但是,它確實讓我們演示了在自定義類上使用堆排序。我們來定義Movie類:
2、實例
from heapq import heappop, heappush
class Movie:
def __init__(self, title, year):
self.title = title
self.year = year
def __str__(self):
return str.format("Title: {}, Year: {}", self.title, self.year)
def __lt__(self, other):
return self.year < other.year
def __gt__(self, other):
return other.__lt__(self)
def __eq__(self, other):
return self.year == other.year
def __ne__(self, other):
return not self.__eq__(other)以上就是python在自定義類上使用堆排序的方法,希望能對大家有所幫助。更多Python學習指路:創(chuàng)新互聯(lián)python教程
本文教程操作環(huán)境:windows7系統(tǒng)、Python 3.9.1,DELL G3電腦。
當前文章:創(chuàng)新互聯(lián)Python教程:python如何在自定義類上使用堆排序
分享地址:http://m.fisionsoft.com.cn/article/dhgchse.html


咨詢
建站咨詢
