File tree Expand file tree Collapse file tree 4 files changed +79
-1
lines changed Expand file tree Collapse file tree 4 files changed +79
-1
lines changed Original file line number Diff line number Diff line change @@ -88,7 +88,7 @@ Python Notes 學習筆記 📝
88
88
89
89
[ __ iter__ tutorial] ( https://github.com/twtrubiks/python-notes/blob/master/__iter__tutorial.py ) - ` __iter__ ` tutorial
90
90
91
- [ _ str_tutorial .py] ( https://github.com/twtrubiks/python-notes/blob/master/_str_tutorial .py ) - ` __str__ ` tutorial
91
+ [ __ str __ tutorial .py] ( https://github.com/twtrubiks/python-notes/blob/master/__str__tutorial .py ) - ` __str__ ` tutorial
92
92
93
93
[ strtobool_tutorial.py] ( https://github.com/twtrubiks/python-notes/blob/master/strtobool_tutorial.py ) - strtobool
94
94
@@ -160,6 +160,8 @@ Python Notes 學習筆記 📝
160
160
161
161
[ __ call__ tutorial.py] ( https://github.com/twtrubiks/python-notes/blob/master/__call__tutorial.py ) - ` __call__ ` tutorial - [ youtube tutorial - What is the ` __call__ ` in python] ( https://youtu.be/YIstPYG15IA )
162
162
163
+ [ queue_tutorial.py] ( queue_tutorial.py ) - queue tutorial
164
+
163
165
## 觀念
164
166
165
167
[ python_circular_import] ( https://github.com/twtrubiks/python-notes/tree/master/python_circular_import ) - [ youtube tutorial - What is Python Circular Imports] ( https://youtu.be/RQhN24QtDAE )
File renamed without changes.
Original file line number Diff line number Diff line change
1
+ from queue import Queue
2
+
3
+
4
+ # if the operation cannot successfully complete because the queue is either empty (cant get) or full ( cant put).
5
+ # The default behavior is to block or idly wait until the Queue object has data or room available to
6
+ # complete the operation.
7
+ # You can have it raise exceptions instead by passing the block=False parameter.
8
+ # Or you can have it wait a defined amount of time before raising an exception by passing a timeout parameter
9
+
10
+ def fifo_ex ():
11
+ # FIFO
12
+ lineup = Queue (maxsize = 3 )
13
+ # lineup.get(block=False)
14
+ lineup .put (1 )
15
+ lineup .put (2 )
16
+ lineup .put (3 )
17
+ # lineup.put(4, timeout=1)
18
+ print ('lineup.full():' , lineup .full ())
19
+ print (lineup .get ())
20
+ print (lineup .get ())
21
+ print (lineup .get ())
22
+ print ('lineup.empty():' , lineup .empty ())
23
+
24
+
25
+ def lifo_ex ():
26
+ # LIFO, also called stacks ()
27
+ # Applicable situation - concurrent
28
+ # why not use list
29
+ from queue import LifoQueue
30
+ stack = LifoQueue (maxsize = 3 )
31
+ stack .put (1 )
32
+ stack .put (2 )
33
+ stack .put (3 )
34
+ # stack.put(4, block=False)
35
+ print (stack .get ())
36
+ print (stack .get ())
37
+ print (stack .get ())
38
+ # stack.get(timeout=1)
39
+
40
+
41
+ def priority_queue_ex ():
42
+ # data structure - heap
43
+ # Applicable situation - product recommendation
44
+ from queue import PriorityQueue
45
+ pq = PriorityQueue ()
46
+ pq .put ((5 , 'c' ))
47
+ pq .put ((2 , 'a' ))
48
+ pq .put ((1 , 'b' ))
49
+ pq .put ((4 , 'd' ))
50
+ while not pq .empty ():
51
+ print (pq .get ())
52
+
53
+
54
+ def priority_queue_ex_2 ():
55
+ from queue import PriorityQueue
56
+ pq = PriorityQueue ()
57
+ pq .put ((- 5 , 'c' ))
58
+ pq .put ((- 2 , 'a' ))
59
+ pq .put ((- 1 , 'b' ))
60
+ pq .put ((- 4 , 'd' ))
61
+ while not pq .empty ():
62
+ print (pq .get ())
63
+
64
+
65
+ if __name__ == "__main__" :
66
+ fifo_ex ()
67
+ # lifo_ex()
68
+ # priority_queue_ex()
69
+ # priority_queue_ex_2()
Original file line number Diff line number Diff line change 11
11
print ('set_seqs:' , set_seqs )
12
12
set_seqs .remove ('test1' )
13
13
print ('set_seqs:' , set_seqs )
14
+
15
+ set_1 = {"a" , "b" , "c" , "d" }
16
+ set_2 = {"d" , "e" , "f" , "g" }
17
+ print ("All: {}" .format (set_1 .union (set_2 )))
18
+ print ("Both: {}" .format (set_1 .intersection (set_2 )))
19
+ print ("Either but not both: {}" .format (
20
+ set_1 .symmetric_difference (set_2 )))
You can’t perform that action at this time.
0 commit comments