python 3.7.3
Use the priority queue using python. I want to apply it a little, such as sorting my own objects.
Also called a priority queue. The scene to use is when you want to sort while the number of elements changes. Or when you want to add an element to the list while keeping the sorted state.
Since various other people have written reference articles, I will omit the details. (https://qiita.com/ell/items/fe52a9eb9499b7060ed6)
Try sorting objects of Name class that have two attributes, first_name and last_name, according to various conditions.
First, generate the following class as a base.
class Name(object):
def __init__(self,first_name,last_name):
self.first_name = first_name
self.last_name = last_name
def __lt__(self,other):
return other.first_name > self.first_name
def __repr__(self):
return self.first_name + " " + self.last_name
The heart of this time is a special method called lt. You can set what it means to be smaller than you. (There are many other special methods as well. Please check them out. https://blog.codecamp.jp/python-class-code)
Let's actually sort using heapq.
from heapq import heapify,heappush,heappop
name1 = Name("james","smith")
name2 = Name("john","johnson")
name3 = Name("robert","williams")
name4 = Name("michael","brown")
name5 = Name("william","jones")
name6 = Name("david","brown")
name7 = Name("james","miller")
name8 = Name("david","davis")
name_list = [name1,name2,name3,name4,name5,name6,name7,name8]
heapify(name_list)
for i in range(len(name_list)):
print(heappop(name_list))
Please refer to the official document for details on how to use heapq. → https://docs.python.org/ja/3/library/heapq.html
Click here for execution results david brown david davis james miller james smith john johnson michael brown robert williams william jones
Since the lt method is set to judge the size based only on the first name, you can see that the first names are arranged in ascending order.
Next, as a practice, try sorting by first name first, then by last name in the opposite direction. The expected output is:
david davis david brown james smith james miller john johnson michael brown robert williams william jones
The first names are listed in ascending order, but the last names are listed in descending order. See the code below for an example implementation of this.
class Name(object):
def __init__(self,first_Name,last_Name):
self.first_Name = first_Name
self.last_Name = last_Name
def __lt__(self,other):
if other.first_Name == self.first_Name:
return other.last_Name < self.last_Name
else:
return other.first_Name > self.first_Name
def __repr__(self):
return self.first_Name +" "+ self.last_Name
from heapq import heapify,heappush,heappop
name1 = Name("james","smith")
name2 = Name("john","johnson")
name3 = Name("robert","williams")
name4 = Name("michael","brown")
name5 = Name("william","jones")
name6 = Name("david","brown")
name7 = Name("james","miller")
name8 = Name("david","davis")
name_list = [name1,name2,name3,name4,name5,name6,name7,name8]
heapify(name_list)
for i in range(len(name_list)):
print(heappop(name_list))
Thank you for watching until the end If you like, please LGTM (Looks Good To Me) If you make a mistake, please point it out.
Recommended Posts