<jupyter_start><jupyter_text>Use lists as stack<jupyter_code>a = [1]
a.append(2)
print(a)
b =a.pop()
print("the pop item is ", b)<jupyter_output>[1, 2]
the pop item is  2<jupyter_text>Use list as queue<jupyter_code>import collections
a = [1, 3, 4, 5]
queue = collections.deque(a)
queue.append(7)
last = queue.popleft()
top = queue[0]
print(queue, last, top)<jupyter_output>deque([3, 4, 5, 7]) 1 3<jupyter_text>Add a tuple in a deque<jupyter_code>import collections
q = collections.deque((1,4)) # can not do this way, it will separate this two items
q.append((1,2))
q.append((3,1))
q<jupyter_output><empty_output><jupyter_text>heapq<jupyter_code>a = [1, 10, 8, 2]
import heapq
heapq.heapify(a)
print(a)
heapq.heappush(a,4)
print(a)
x = heapq.heappop(a)
print(x)<jupyter_output>[1, 2, 8, 10]
[1, 2, 8, 10, 4]
1<jupyter_text>heapq, n largest and n smallest<jupyter_code>heap = [3,5,6,1,2,0]
import heapq
heapq.heapify(heap)
large = heapq.nlargest(3, heap)
small = heapq.nsmallest(2, heap)
print(large, small)
heap = []
import heapq
print(heap)
heapq.heappush(heap, 1)
print(heap)
heapq.heappush(heap, 2)
print(heap)
heapq.heappop(heap)
heap[0]<jupyter_output>[]
[1]
[1, 2]<jupyter_text>The input could be a tuple<jupyter_code>a = [(5, "review"), (7, "pratice"),(1, "writting homework")]
import heapq
heapq.heapify(a)
print(heapq.heappop(a))
a = []
import heapq
heapq.heappush(a,(7, "pratice"))
heapq.heappush(a,(1, "writting homework"))
print(a)
print(heapq.heappop(a))<jupyter_output>[(1, 'writting homework'), (7, 'pratice')]
(1, 'writting homework')<jupyter_text>New class and comparator implementationSort based on grade and then name<jupyter_code>d = [("Peter", 12, 'A'), ("Lu", 10, 'B'), ("James", 7, 'C'), ("Jemery", 8, 'A')]
class Student:
    def __init__(self, name ='', age = 0, grade = ''):
        self.name = name
        self.age = age
        self.grade = grade

    def __lt__(self, other):
        if self.grade == other.grade:
            return self.name <= other.name
        else:
            return self.grade < other.grade

data = [Student(i, j, k) for i,j,k in d]
heapq.heapify(data)
[heapq.heappop(data).name for _ in range(len(d))]

# we can also use a sorted list for this kind of problems

p = []
import heapq
heapq.heappush(p, 2)
heapq.heappush(p, 1)
p[0]<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.1699826989619377, 'fraction_numerical': 0.03157439446366782, 'mean_word_length': 5.070866141732283, 'pattern_counts': {'":': 0, '<': 26, '<?xml version=': 0, '>': 24, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '7', 'dir': 'jupyter-structured-clean-dedup', 'id': '546913', 'license': 'MIT', 'n_tokens_mistral': 1014, 'n_tokens_neox': 970, 'n_words': 234, 'path': 'Data Structure - Stack, Deque, Heapq.ipynb', 'repo_name': 'yuzhipeter/Data_Structure_Numpy_Pandas', 'size': '2312'}