{"id":1579,"date":"2023-03-25T11:33:17","date_gmt":"2023-03-25T03:33:17","guid":{"rendered":""},"modified":"2023-03-25T11:33:17","modified_gmt":"2023-03-25T03:33:17","slug":"Python\u7ebf\u7a0b\u901a\u4fe1","status":"publish","type":"post","link":"https:\/\/bianchenghao6.com\/1579.html","title":{"rendered":"Python\u7ebf\u7a0b\u901a\u4fe1"},"content":{"rendered":"
\n
Python\u7ebf\u7a0b\u901a\u4fe1\u8be6\u7ec6\u64cd\u4f5c\u6559\u7a0b<\/span>\n <\/div>\n \u6ca1\u6709\u6027\u80fd\u63d0\u5347<\/b>-\u5982\u679c\u6211\u4eec\u65e0\u6cd5\u5728\u7ebf\u7a0b\u548c\u8fdb\u7a0b\u4e4b\u95f4\u5b9e\u73b0\u6b63\u786e\u7684\u901a\u4fe1\uff0c\u90a3\u4e48\u5e76\u53d1\u548c\u5e76\u884c\u6027\u5e26\u6765\u7684\u6027\u80fd\u63d0\u5347\u5c31\u6ca1\u6709\u7528\u4e86\u3002<\/span> L\uff0cL1\uff0cL2\u90fd\u662f\u5217\u8868<\/span> \u666e\u901a\u961f\u5217\uff08\u5148\u8fdb\u5148\u51fa\uff0c\u5148\u8fdb\u5148\u51fa\uff09<\/span>
\n \u6b63\u786e\u5b8c\u6210\u4efb\u52a1<\/b>-\u7ebf\u7a0b\u4e4b\u95f4\u6ca1\u6709\u9002\u5f53\u7684\u4e92\u901a\u673a\u5236\uff0c\u5206\u914d\u7684\u4efb\u52a1\u5c06\u65e0\u6cd5\u6b63\u786e\u5b8c\u6210\u3002<\/span>
\n \u6bd4\u8fdb\u7a0b\u95f4\u901a\u4fe1\u66f4\u6709\u6548<\/b>-\u7ebf\u7a0b\u95f4\u901a\u4fe1\u6bd4\u8fdb\u7a0b\u95f4\u901a\u4fe1\u66f4\u9ad8\u6548\uff0c\u66f4\u6613\u4e8e\u4f7f\u7528\uff0c\u56e0\u4e3a\u8fdb\u7a0b\u5185\u7684\u6240\u6709\u7ebf\u7a0b\u5171\u4eab\u76f8\u540c\u7684\u5730\u5740\u7a7a\u95f4\uff0c\u800c\u4e0d\u5fc5\u4f7f\u7528\u5171\u4eab\u7684\u8bb0\u5fc6\u3002<\/span> <\/p>\n\u7528\u4e8e\u7ebf\u7a0b\u5b89\u5168\u901a\u4fe1\u7684Python\u6570\u636e\u7ed3\u6784<\/h2>\n
\u96c6\u5408<\/h3>\n
\u793a\u4f8b<\/h3>\n
# Filename : example.py<\/span>
# Copyright : 2020 By Bianchenghao6<\/span>
# Author by : bianchenghao6.com<\/span>
# Date : 2020-08-22<\/span>
class <\/span>extend_class(set):
def <\/span>__init__(self, *args, **kwargs):
self._lock = Lock<\/span>()
super(extend_class, self).__init__<\/span>(*args, **kwargs)
def <\/span>add(self, elem):
self._lock.acquire<\/span>()
try:
super(extend_class, self).add<\/span>(elem)
finally:
self._lock.release<\/span>()
def <\/span>delete(self, elem):
self._lock.acquire<\/span>()
try:
super(extend_class, self).delete<\/span>(elem)
finally:
self._lock.release<\/span>()
<\/span><\/code><\/pre>\n<\/p><\/div>\n
\n extend_class <\/b>\u7684\u7c7b\u5bf9\u8c61\uff0c\u8be5\u5bf9\u8c61\u8fdb\u4e00\u6b65\u4ecePython
\n set\u7c7b<\/b>\u7ee7\u627f\u3002\u5728\u6b64\u7c7b\u7684\u6784\u9020\u51fd\u6570\u4e2d\u521b\u5efa\u4e00\u4e2a\u9501\u5bf9\u8c61\u3002\u73b0\u5728\uff0c\u6709\u4e24\u4e2a\u51fd\u6570-
\n add()<\/b>\u548c
\n delete()<\/b>\u3002\u8fd9\u4e9b\u51fd\u6570\u5df2\u5b9a\u4e49\u5e76\u4e14\u662f\u7ebf\u7a0b\u5b89\u5168\u7684\u3002\u5b83\u4eec\u90fd\u4f9d\u8d56
\n super <\/b>\u7c7b\u529f\u80fd\uff0c\u4f46\u6709\u4e00\u4e2a\u5173\u952e\u7684\u4f8b\u5916\u3002\n <\/div>\n\u88c5\u9970\u5668<\/h3>\n
\u793a\u4f8b<\/h3>\n
# Filename : example.py<\/span>
# Copyright : 2020 By Bianchenghao6<\/span>
# Author by : bianchenghao6.com<\/span>
# Date : 2020-08-22<\/span>
def <\/span>lock_decorator(method):
def <\/span>new_deco_method(self, *args, **kwargs):
with <\/span>self._lock:
return <\/span>method(self, *args, **kwargs)
return <\/span>new_deco_method
class <\/span>Decorator_class(set):
def <\/span>__init__(self, *args, **kwargs):
self._lock = Lock<\/span>()
super(Decorator_class, self).__init__<\/span>(*args, **kwargs)
@lock_decorator
def <\/span>add(self, *args, **kwargs):
return <\/span>super(Decorator_class, self).add<\/span>(elem)
@lock_decorator
def <\/span>delete(self, *args, **kwargs):
return <\/span>super(Decorator_class, self).delete<\/span>(elem)
<\/span><\/code><\/pre>\n<\/p><\/div>\n\u5217\u8868<\/h3>\n
\n L.append\uff08x\uff09<\/b>\u4e0d\u80fd\u4fdd\u8bc1\u8fd4\u56de\u9884\u671f\u7684\u7ed3\u679c\u3002\u8fd9\u662f\u56e0\u4e3a\uff0c\u5c3d\u7ba1
\n append()<\/b>\u662f\u539f\u5b50\u64cd\u4f5c\u4e14\u7ebf\u7a0b\u5b89\u5168\u7684\uff0c\u4f46\u53e6\u4e00\u4e2a\u7ebf\u7a0b\u6b63\u5728\u5c1d\u8bd5\u4ee5\u5e76\u53d1\u65b9\u5f0f\u4fee\u6539\u5217\u8868\u7684\u6570\u636e\uff0c\u56e0\u6b64\u6211\u4eec\u53ef\u4ee5\u770b\u5230\u7ade\u4e89\u6761\u4ef6\u5bf9\u8f93\u51fa\u7684\u526f\u4f5c\u7528\n <\/div>\n # Filename : example.py<\/span>
# Copyright : 2020 By Bianchenghao6<\/span>
# Author by : bianchenghao6.com<\/span>
# Date : 2020-08-22<\/span>
L.append<\/span>(x)
L1.extend<\/span>(L2)
x = L[i]
x = L.pop<\/span>()
L1[i:j] = L2
L.sort<\/span>()
x = y
x.field = y
D[x] = y
D1.update<\/span>(D2)
D.keys<\/span>()
<\/span><\/code><\/pre>\n<\/p><\/div>\n
\n D\uff0cD1\uff0cD2\u662f\u5b57\u5178<\/span>
\n x\uff0cy\u662f\u5bf9\u8c61<\/span>
\n i\uff0cj\u662f\u6574\u6570<\/span> <\/p>\n\u961f\u5217<\/h3>\n
<\/p>\n
\n <\u961f\u5217>
\n \u6a21\u5757\u4ee5\u5728\u6211\u4eec\u7684\u5e94\u7528\u7a0b\u5e8f\u4e2d\u4f7f\u7528\u4e0d\u540c\u7c7b\u578b\u7684\u961f\u5217\u3002
\n
\n <\/\u961f\u5217>\n <\/div>\n\u961f\u5217\u7c7b\u578b<\/h2>\n
\n
\n
\n <\/queue><\/b>\u6a21\u5757\u4e2d\u63d0\u4f9b\u4e86\u4e09\u79cd\u53ef\u4f9b\u4f7f\u7528\u7684\u961f\u5217\u9009\u9879-\n <\/div>\n
\n \u540e\u8fdb\u5148\u51fa\uff0c\u540e\u8fdb\u5148\u51fa<\/span>
\n \u4f18\u5148<\/span> <\/p>\n\u666e\u901a\u961f\u5217\uff08\u5148\u8fdb\u5148\u51fa\uff0c\u5148\u8fdb\u5148\u51fa\uff09<\/h2>\n
<\/p>\n
FIFO\u961f\u5217\u7684Python\u5b9e\u73b0<\/h3>\n
\u5177\u6709\u5355\u7ebf\u7a0b\u7684FIFO\u961f\u5217<\/h3>\n
\n Queue <\/b>\u7c7b\u5c06\u5b9e\u73b0\u4e00\u4e2a\u57fa\u672c\u7684\u5148\u8fdb\u5148\u51fa\u5bb9\u5668\u3002\u5143\u7d20\u5c06\u4f7f\u7528
\n put()<\/b>\u6dfb\u52a0\u5230\u5e8f\u5217\u7684\u4e00\u4e2a\"\u672b\u7aef\"\uff0c\u5e76\u4f7f\u7528
\n get()<\/b>\u4ece\u53e6\u4e00\u672b\u7aef\u9664\u53bb\u3002\n <\/div>\n\u793a\u4f8b<\/h3>\n
# Filename : example.py<\/span>
# Copyright : 2020 By Bianchenghao6<\/span>
# Author by : bianchenghao6.com<\/span>
# Date : 2020-08-22<\/span>
import <\/span>queue
q = queue.Queue<\/span>()
for <\/span><\/span>i in <\/span>range(8):
q.put<\/span>(\"item-\"<\/span> + str(i))
while <\/span>not <\/span>q.empty<\/span>():
print <\/span>(q.get<\/span>(), end = \" \"<\/span>)
<\/span><\/code><\/pre>\n<\/p><\/div>\n\u8f93\u51fa<\/h3>\n
# Filename : example.py<\/span>
# Copyright : 2020 By Bianchenghao6<\/span>
# Author by : bianchenghao6.com<\/span>
# Date : 2020-08-22<\/span>
item-0 item-1 item-2 item-3 item-4 item-5 item-6 item-7
<\/span><\/code><\/pre>\n<\/p><\/div>\n\u5177\u6709\u591a\u4e2a\u7ebf\u7a0b\u7684FIFO\u961f\u5217<\/h3>\n
\u793a\u4f8b<\/h3>\n
# Filename : example.py<\/span>
# Copyright : 2020 By Bianchenghao6<\/span>
# Author by : bianchenghao6.com<\/span>
# Date : 2020-08-22<\/span>
import <\/span>threading
import <\/span>queue
import <\/span>random
import <\/span>time
def <\/span>myqueue(queue):
while <\/span>not <\/span>queue.empty<\/span>():
item = queue.get<\/span>()
if <\/span>item is <\/span>None:
break<\/span>
print(\"{} removed {} from <\/span>the queue\"<\/span>.format<\/span>(threading.current_thread<\/span>(), item))
queue.task_done<\/span>()
time.sleep<\/span>(2)
q = queue.Queue<\/span>()
for <\/span><\/span>i in <\/span>range(5):
q.put<\/span>(i)
threads = []
for <\/span><\/span>i in <\/span>range(4):
thread = threading.Thread<\/span>(target=myqueue, args=(q,))
thread.start<\/span>()
threads.append<\/span>(thread)
for <\/span><\/span>thread in <\/span>threads:
thread.join<\/span>()
<\/span><\/code><\/pre>\n<\/p><\/div>\n\u8f93\u51fa<\/h3>\n
<Thread(Thread-3654, started 5044)> removed 0 from <\/span>the queue
<Thread(Thread-3655, started 3144)> removed 1 from <\/span>the queue
<Thread(Thread-3656, started 6996)> removed 2 from <\/span>the queue
<Thread(Thread-3657, started 2672)> removed 3 from <\/span>the queue
<Thread(Thread-3654, started 5044)> removed 4 from <\/span>the queue
<\/span><\/code><\/pre>\n<\/p><\/div>\n LIFO\uff0c\u540e\u8fdb\u5148\u51fa\u961f\u5217<\/h2>\n
LIFO\u961f\u5217\u7684Python\u5b9e\u73b0<\/h3>\n
\u5355\u7ebf\u7a0bLIFO\u961f\u5217<\/h3>\n
\n Queue <\/b>\u7c7b\u5c06\u4f7f\u7528\u7ed3\u6784
\n Queue <\/b> .LifoQueue\u5b9e\u73b0\u4e00\u4e2a\u57fa\u672c\u7684\u540e\u8fdb\u5148\u51fa\u5bb9\u5668\u3002\u73b0\u5728\uff0c\u5728\u8c03\u7528
\n put()<\/b>\u65f6\uff0c\u4e5f\u53ef\u200b\u200b\u4ee5\u4f7f\u7528
\n get()<\/b>\u5c06\u5143\u7d20\u6dfb\u52a0\u5230\u5bb9\u5668\u7684\u5934\u90e8\uff0c\u5e76\u4ece\u5934\u90e8\u79fb\u9664\u3002\n <\/div>\n\u793a\u4f8b<\/h3>\n
# Filename : example.py<\/span>
# Copyright : 2020 By Bianchenghao6<\/span>
# Author by : bianchenghao6.com<\/span>
# Date : 2020-08-22<\/span>
import <\/span>queue
q = queue.LifoQueue<\/span>()
for <\/span><\/span>i in <\/span>range(8):
q.put<\/span>(\"item-\"<\/span> + str(i))
while <\/span>not <\/span>q.empty<\/span>():
print <\/span>(q.get<\/span>(), end=\" \"<\/span>)
Output:
item-7 item-6 item-5 item-4 item-3 item-2 item-1 item-0
<\/span><\/code><\/pre>\n<\/p><\/div>\n\u5177\u6709\u591a\u4e2a\u7ebf\u7a0b\u7684LIFO\u961f\u5217<\/h3>\n
\n Queue <\/b>\u7c7b\uff0c\u8be5\u7c7b\u5c06\u901a\u8fc7\u4f7f\u7528
\n Queue.LifoQueue <\/b>\u7ed3\u6784\u5b9e\u73b0\u4e00\u4e2a\u57fa\u672c\u7684\u540e\u8fdb\u5148\u51fa\u5bb9\u5668\u3002\n <\/div>\n\u793a\u4f8b<\/h3>\n
# Filename : example.py<\/span>
# Copyright : 2020 By Bianchenghao6<\/span>
# Author by : bianchenghao6.com<\/span>
# Date : 2020-08-22<\/span>