{"id":1173829,"date":"2025-01-15T17:08:37","date_gmt":"2025-01-15T09:08:37","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1173829.html"},"modified":"2025-01-15T17:08:41","modified_gmt":"2025-01-15T09:08:41","slug":"python%e5%a6%82%e4%bd%95%e5%ae%9a%e4%b9%89%e5%ae%9a%e9%95%bf%e7%9a%84list-2","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1173829.html","title":{"rendered":"python\u5982\u4f55\u5b9a\u4e49\u5b9a\u957f\u7684list"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/26075508\/729959fb-06bd-4db2-9680-d6db4ef22dc8.webp\" alt=\"python\u5982\u4f55\u5b9a\u4e49\u5b9a\u957f\u7684list\" \/><\/p>\n<p><p> <strong>Python\u5b9a\u4e49\u5b9a\u957f\u7684list\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u51e0\u79cd\u65b9\u6cd5\uff1a\u4f7f\u7528\u5217\u8868\u4e58\u6cd5\u3001\u4f7f\u7528numpy\u5e93\u3001\u4f7f\u7528collections.deque\u3002<\/strong> \u5176\u4e2d\uff0c\u4f7f\u7528numpy\u5e93\u521b\u5efa\u5b9a\u957f\u7684\u6570\u7ec4\u662f\u6bd4\u8f83\u5e38\u7528\u548c\u9ad8\u6548\u7684\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<p><p>\u4f8b\u5982\uff0c\u4f7f\u7528numpy\u5e93\u53ef\u4ee5\u901a\u8fc7\u5982\u4e0b\u4ee3\u7801\u521b\u5efa\u4e00\u4e2a\u5b9a\u957f\u7684\u6570\u7ec4\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import numpy as np<\/p>\n<p>fixed_length_array = np.zeros(10)  # \u521b\u5efa\u4e00\u4e2a\u957f\u5ea6\u4e3a10\u7684\u6570\u7ec4\uff0c\u521d\u59cb\u503c\u4e3a0<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u63a5\u4e0b\u6765\uff0c\u6211\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u8fd9\u51e0\u79cd\u65b9\u6cd5\uff0c\u5e76\u5c55\u793a\u5982\u4f55\u5728\u4e0d\u540c\u7684\u573a\u666f\u4e2d\u6709\u6548\u5730\u4f7f\u7528\u5b9a\u957f\u5217\u8868\u3002<\/p>\n<\/p>\n<p><h2>\u4e00\u3001\u5217\u8868\u4e58\u6cd5<\/h2>\n<\/p>\n<p><h3>\u4f7f\u7528\u5217\u8868\u4e58\u6cd5\u521b\u5efa\u5b9a\u957f\u5217\u8868<\/h3>\n<\/p>\n<p><p>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u901a\u8fc7\u5217\u8868\u4e58\u6cd5\u6765\u521b\u5efa\u4e00\u4e2a\u5b9a\u957f\u7684\u5217\u8868\u3002\u5217\u8868\u4e58\u6cd5\u662f\u6307\u5c06\u4e00\u4e2a\u5217\u8868\u4e58\u4ee5\u4e00\u4e2a\u6574\u6570\uff0c\u4ece\u800c\u521b\u5efa\u4e00\u4e2a\u5305\u542b\u91cd\u590d\u5143\u7d20\u7684\u5217\u8868\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">fixed_length_list = [0] * 10  # \u521b\u5efa\u4e00\u4e2a\u957f\u5ea6\u4e3a10\u7684\u5217\u8868\uff0c\u521d\u59cb\u503c\u4e3a0<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u79cd\u65b9\u6cd5\u7b80\u5355\u76f4\u89c2\uff0c\u975e\u5e38\u9002\u5408\u521b\u5efa\u5305\u542b\u76f8\u540c\u521d\u59cb\u503c\u7684\u5b9a\u957f\u5217\u8868\u3002<\/p>\n<\/p>\n<p><h3>\u4fee\u6539\u5b9a\u957f\u5217\u8868\u7684\u5185\u5bb9<\/h3>\n<\/p>\n<p><p>\u521b\u5efa\u5b9a\u957f\u5217\u8868\u4e4b\u540e\uff0c\u53ef\u4ee5\u50cf\u64cd\u4f5c\u666e\u901a\u5217\u8868\u4e00\u6837\u4fee\u6539\u5176\u5185\u5bb9\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">fixed_length_list[2] = 5  # \u5c06\u5217\u8868\u7684\u7b2c\u4e09\u4e2a\u5143\u7d20\u4fee\u6539\u4e3a5<\/p>\n<p>print(fixed_length_list)  # \u8f93\u51fa: [0, 0, 5, 0, 0, 0, 0, 0, 0, 0]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5217\u8868\u4e58\u6cd5\u7684\u4e00\u4e2a\u7f3a\u70b9\u662f\uff0c\u5982\u679c\u5217\u8868\u4e2d\u7684\u5143\u7d20\u662f\u53ef\u53d8\u5bf9\u8c61\uff08\u4f8b\u5982\u5217\u8868\u3001\u5b57\u5178\uff09\uff0c\u90a3\u4e48\u6240\u6709\u4f4d\u7f6e\u5c06\u5f15\u7528\u540c\u4e00\u4e2a\u5bf9\u8c61\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">fixed_length_list_of_lists = [[]] * 5<\/p>\n<p>fixed_length_list_of_lists[0].append(1)<\/p>\n<p>print(fixed_length_list_of_lists)  # \u8f93\u51fa: [[1], [1], [1], [1], [1]]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4e3a\u4e86\u907f\u514d\u8fd9\u79cd\u60c5\u51b5\uff0c\u53ef\u4ee5\u4f7f\u7528\u5217\u8868\u751f\u6210\u5f0f\u6765\u521b\u5efa\u72ec\u7acb\u7684\u53ef\u53d8\u5bf9\u8c61\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">fixed_length_list_of_lists = [[] for _ in range(5)]<\/p>\n<p>fixed_length_list_of_lists[0].append(1)<\/p>\n<p>print(fixed_length_list_of_lists)  # \u8f93\u51fa: [[1], [], [], [], []]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h2>\u4e8c\u3001\u4f7f\u7528numpy\u5e93<\/h2>\n<\/p>\n<p><h3>\u521b\u5efa\u5b9a\u957f\u6570\u7ec4<\/h3>\n<\/p>\n<p><p>\u4f7f\u7528numpy\u5e93\u53ef\u4ee5\u65b9\u4fbf\u5730\u521b\u5efa\u548c\u64cd\u4f5c\u5b9a\u957f\u6570\u7ec4\u3002numpy\u6570\u7ec4\u5728\u6570\u503c\u8ba1\u7b97\u4e2d\u975e\u5e38\u9ad8\u6548\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import numpy as np<\/p>\n<p>fixed_length_array = np.zeros(10)  # \u521b\u5efa\u4e00\u4e2a\u957f\u5ea6\u4e3a10\u7684\u6570\u7ec4\uff0c\u521d\u59cb\u503c\u4e3a0<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4fee\u6539\u5b9a\u957f\u6570\u7ec4\u7684\u5185\u5bb9<\/h3>\n<\/p>\n<p><p>\u53ef\u4ee5\u4f7f\u7528\u7d22\u5f15\u6765\u4fee\u6539numpy\u6570\u7ec4\u7684\u5185\u5bb9\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">fixed_length_array[3] = 7<\/p>\n<p>print(fixed_length_array)  # \u8f93\u51fa: [0. 0. 0. 7. 0. 0. 0. 0. 0. 0.]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u521b\u5efa\u4e0d\u540c\u7c7b\u578b\u7684\u6570\u7ec4<\/h3>\n<\/p>\n<p><p>numpy\u63d0\u4f9b\u4e86\u591a\u79cd\u521b\u5efa\u6570\u7ec4\u7684\u65b9\u6cd5\uff0c\u53ef\u4ee5\u521b\u5efa\u4e0d\u540c\u7c7b\u578b\u7684\u6570\u7ec4\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">fixed_length_int_array = np.zeros(10, dtype=int)  # \u521b\u5efa\u4e00\u4e2a\u957f\u5ea6\u4e3a10\u7684\u6574\u6570\u6570\u7ec4<\/p>\n<p>fixed_length_float_array = np.ones(5, dtype=float)  # \u521b\u5efa\u4e00\u4e2a\u957f\u5ea6\u4e3a5\u7684\u6d6e\u70b9\u6570\u6570\u7ec4\uff0c\u521d\u59cb\u503c\u4e3a1<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u9ad8\u6548\u7684\u6570\u503c\u8ba1\u7b97<\/h3>\n<\/p>\n<p><p>numpy\u6570\u7ec4\u5728\u6570\u503c\u8ba1\u7b97\u4e2d\u975e\u5e38\u9ad8\u6548\uff0c\u9002\u5408\u5904\u7406\u5927\u89c4\u6a21\u6570\u636e\u3002\u4f8b\u5982\uff0c\u8ba1\u7b97\u6570\u7ec4\u5143\u7d20\u7684\u548c\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">array_sum = np.sum(fixed_length_array)<\/p>\n<p>print(array_sum)  # \u8f93\u51fa\u6570\u7ec4\u5143\u7d20\u7684\u548c<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h2>\u4e09\u3001\u4f7f\u7528collections.deque<\/h2>\n<\/p>\n<p><h3>\u521b\u5efa\u5b9a\u957fdeque<\/h3>\n<\/p>\n<p><p>collections\u6a21\u5757\u63d0\u4f9b\u4e86deque\uff08\u53cc\u7aef\u961f\u5217\uff09\uff0c\u53ef\u4ee5\u6307\u5b9a\u6700\u5927\u957f\u5ea6\u3002\u5f53\u5143\u7d20\u6570\u91cf\u8d85\u8fc7\u6700\u5927\u957f\u5ea6\u65f6\uff0c\u6700\u65e7\u7684\u5143\u7d20\u4f1a\u88ab\u81ea\u52a8\u79fb\u9664\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from collections import deque<\/p>\n<p>fixed_length_deque = deque(maxlen=10)  # \u521b\u5efa\u4e00\u4e2a\u6700\u5927\u957f\u5ea6\u4e3a10\u7684deque<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u5411deque\u6dfb\u52a0\u5143\u7d20<\/h3>\n<\/p>\n<p><p>\u53ef\u4ee5\u4f7f\u7528append\u65b9\u6cd5\u5411deque\u6dfb\u52a0\u5143\u7d20\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">for i in range(12):<\/p>\n<p>    fixed_length_deque.append(i)<\/p>\n<p>    print(fixed_length_deque)  # \u8f93\u51fa: deque([2, 3, 4, 5, 6, 7, 8, 9, 10, 11], maxlen=10)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>deque\u7684\u7279\u70b9<\/h3>\n<\/p>\n<p><p>deque\u4e0e\u5217\u8868\u76f8\u6bd4\u6709\u4e00\u4e9b\u72ec\u7279\u7684\u7279\u70b9\uff1a<\/p>\n<\/p>\n<ol>\n<li><strong>\u53cc\u7aef\u64cd\u4f5c<\/strong>\uff1adeque\u652f\u6301\u5728\u4e24\u7aef\u9ad8\u6548\u5730\u6dfb\u52a0\u548c\u5220\u9664\u5143\u7d20\u3002<\/li>\n<li><strong>\u81ea\u52a8\u79fb\u9664\u65e7\u5143\u7d20<\/strong>\uff1a\u5f53\u5143\u7d20\u6570\u91cf\u8d85\u8fc7\u6700\u5927\u957f\u5ea6\u65f6\uff0c\u6700\u65e7\u7684\u5143\u7d20\u4f1a\u88ab\u81ea\u52a8\u79fb\u9664\u3002<\/li>\n<\/ol>\n<p><h2>\u56db\u3001\u5b9a\u957f\u5217\u8868\u7684\u5b9e\u9645\u5e94\u7528<\/h2>\n<\/p>\n<p><h3>\u961f\u5217\u5b9e\u73b0<\/h3>\n<\/p>\n<p><p>\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u5b9a\u957f\u5217\u8868\u5e38\u7528\u4e8e\u5b9e\u73b0\u961f\u5217\u3002\u961f\u5217\u662f\u4e00\u79cd\u5148\u8fdb\u5148\u51fa\uff08FIFO\uff09\u7684\u6570\u636e\u7ed3\u6784\uff0c\u53ef\u4ee5\u4f7f\u7528collections.deque\u6765\u5b9e\u73b0\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">class FixedLengthQueue:<\/p>\n<p>    def __init__(self, maxlen):<\/p>\n<p>        self.queue = deque(maxlen=maxlen)<\/p>\n<p>    def enqueue(self, item):<\/p>\n<p>        self.queue.append(item)<\/p>\n<p>    def dequeue(self):<\/p>\n<p>        if self.queue:<\/p>\n<p>            return self.queue.popleft()<\/p>\n<p>        else:<\/p>\n<p>            r<a href=\"https:\/\/docs.pingcode.com\/blog\/59162.html\" target=\"_blank\">AI<\/a>se IndexError(&quot;dequeue from an empty queue&quot;)<\/p>\n<p>    def __repr__(self):<\/p>\n<p>        return repr(self.queue)<\/p>\n<h2><strong>\u793a\u4f8b<\/strong><\/h2>\n<p>queue = FixedLengthQueue(3)<\/p>\n<p>queue.enqueue(1)<\/p>\n<p>queue.enqueue(2)<\/p>\n<p>queue.enqueue(3)<\/p>\n<p>queue.enqueue(4)  # \u961f\u5217\u6ee1\u65f6\uff0c\u6700\u65e7\u7684\u5143\u7d201\u5c06\u88ab\u79fb\u9664<\/p>\n<p>print(queue)  # \u8f93\u51fa: deque([2, 3, 4], maxlen=3)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u6ed1\u52a8\u7a97\u53e3<\/h3>\n<\/p>\n<p><p>\u5b9a\u957f\u5217\u8868\u4e5f\u5e38\u7528\u4e8e\u5b9e\u73b0\u6ed1\u52a8\u7a97\u53e3\u3002\u6ed1\u52a8\u7a97\u53e3\u662f\u4e00\u79cd\u5e38\u89c1\u7684\u7b97\u6cd5\u6280\u5de7\uff0c\u7528\u4e8e\u5904\u7406\u8fde\u7eed\u5b50\u6570\u7ec4\u6216\u5b50\u5e8f\u5217\u7684\u95ee\u9898\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def sliding_window_average(arr, window_size):<\/p>\n<p>    if window_size &gt; len(arr):<\/p>\n<p>        raise ValueError(&quot;window_size should be less than or equal to the length of the array&quot;)<\/p>\n<p>    window = deque(maxlen=window_size)<\/p>\n<p>    result = []<\/p>\n<p>    for num in arr:<\/p>\n<p>        window.append(num)<\/p>\n<p>        if len(window) == window_size:<\/p>\n<p>            result.append(sum(window) \/ window_size)<\/p>\n<p>    return result<\/p>\n<h2><strong>\u793a\u4f8b<\/strong><\/h2>\n<p>arr = [1, 2, 3, 4, 5, 6, 7, 8, 9]<\/p>\n<p>window_size = 3<\/p>\n<p>averages = sliding_window_average(arr, window_size)<\/p>\n<p>print(averages)  # \u8f93\u51fa: [2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u6570\u636e\u7f13\u5b58<\/h3>\n<\/p>\n<p><p>\u5b9a\u957f\u5217\u8868\u8fd8\u53ef\u4ee5\u7528\u4e8e\u5b9e\u73b0\u6570\u636e\u7f13\u5b58\u3002\u4f8b\u5982\uff0c\u7f13\u5b58\u6700\u8fd1\u8bbf\u95ee\u7684\u7f51\u9875\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">class WebPageCache:<\/p>\n<p>    def __init__(self, maxlen):<\/p>\n<p>        self.cache = deque(maxlen=maxlen)<\/p>\n<p>    def visit_page(self, page_url):<\/p>\n<p>        if page_url in self.cache:<\/p>\n<p>            self.cache.remove(page_url)<\/p>\n<p>        self.cache.append(page_url)<\/p>\n<p>    def get_recent_pages(self):<\/p>\n<p>        return list(self.cache)<\/p>\n<h2><strong>\u793a\u4f8b<\/strong><\/h2>\n<p>cache = WebPageCache(3)<\/p>\n<p>cache.visit_page(&quot;page1&quot;)<\/p>\n<p>cache.visit_page(&quot;page2&quot;)<\/p>\n<p>cache.visit_page(&quot;page3&quot;)<\/p>\n<p>cache.visit_page(&quot;page4&quot;)  # \u7f13\u5b58\u6ee1\u65f6\uff0c\u6700\u65e7\u7684\u9875\u9762page1\u5c06\u88ab\u79fb\u9664<\/p>\n<p>print(cache.get_recent_pages())  # \u8f93\u51fa: [&#39;page2&#39;, &#39;page3&#39;, &#39;page4&#39;]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u6570\u636e\u6d41\u5904\u7406<\/h3>\n<\/p>\n<p><p>\u5728\u6570\u636e\u6d41\u5904\u7406\u4e2d\uff0c\u5b9a\u957f\u5217\u8868\u53ef\u4ee5\u7528\u4e8e\u5b58\u50a8\u6700\u8fd1\u7684N\u4e2a\u6570\u636e\u70b9\u3002\u4f8b\u5982\uff0c\u5b9e\u65f6\u76d1\u63a7\u7cfb\u7edf\u53ef\u4ee5\u4f7f\u7528\u5b9a\u957f\u5217\u8868\u6765\u5b58\u50a8\u6700\u8fd1\u7684\u4f20\u611f\u5668\u8bfb\u6570\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">class SensorDataStream:<\/p>\n<p>    def __init__(self, maxlen):<\/p>\n<p>        self.data_stream = deque(maxlen=maxlen)<\/p>\n<p>    def add_data(self, data_point):<\/p>\n<p>        self.data_stream.append(data_point)<\/p>\n<p>    def get_recent_data(self):<\/p>\n<p>        return list(self.data_stream)<\/p>\n<h2><strong>\u793a\u4f8b<\/strong><\/h2>\n<p>stream = SensorDataStream(5)<\/p>\n<p>stream.add_data(10)<\/p>\n<p>stream.add_data(12)<\/p>\n<p>stream.add_data(15)<\/p>\n<p>stream.add_data(18)<\/p>\n<p>stream.add_data(20)<\/p>\n<p>stream.add_data(25)  # \u6570\u636e\u6ee1\u65f6\uff0c\u6700\u65e7\u7684\u6570\u636e\u70b910\u5c06\u88ab\u79fb\u9664<\/p>\n<p>print(stream.get_recent_data())  # \u8f93\u51fa: [12, 15, 18, 20, 25]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u5b9e\u73b0\u73af\u5f62\u7f13\u51b2\u533a<\/h3>\n<\/p>\n<p><p>\u5b9a\u957f\u5217\u8868\u8fd8\u53ef\u4ee5\u7528\u4e8e\u5b9e\u73b0\u73af\u5f62\u7f13\u51b2\u533a\uff08Circular Buffer\uff09\uff0c\u8fd9\u5728\u9700\u8981\u56fa\u5b9a\u5927\u5c0f\u7684\u7f13\u51b2\u533a\u4ee5\u652f\u6301\u5faa\u73af\u5199\u5165\u548c\u8bfb\u53d6\u7684\u573a\u666f\u4e2d\u975e\u5e38\u6709\u7528\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">class CircularBuffer:<\/p>\n<p>    def __init__(self, size):<\/p>\n<p>        self.size = size<\/p>\n<p>        self.buffer = [None] * size<\/p>\n<p>        self.start = 0<\/p>\n<p>        self.end = 0<\/p>\n<p>        self.count = 0<\/p>\n<p>    def append(self, item):<\/p>\n<p>        self.buffer[self.end] = item<\/p>\n<p>        self.end = (self.end + 1) % self.size<\/p>\n<p>        if self.count &lt; self.size:<\/p>\n<p>            self.count += 1<\/p>\n<p>        else:<\/p>\n<p>            self.start = (self.start + 1) % self.size<\/p>\n<p>    def get(self):<\/p>\n<p>        return [self.buffer[(self.start + i) % self.size] for i in range(self.count)]<\/p>\n<p>    def __repr__(self):<\/p>\n<p>        return repr(self.get())<\/p>\n<h2><strong>\u793a\u4f8b<\/strong><\/h2>\n<p>circular_buffer = CircularBuffer(3)<\/p>\n<p>circular_buffer.append(1)<\/p>\n<p>circular_buffer.append(2)<\/p>\n<p>circular_buffer.append(3)<\/p>\n<p>print(circular_buffer)  # \u8f93\u51fa: [1, 2, 3]<\/p>\n<p>circular_buffer.append(4)  # \u7f13\u51b2\u533a\u6ee1\u65f6\uff0c\u6700\u65e7\u7684\u5143\u7d201\u5c06\u88ab\u8986\u76d6<\/p>\n<p>print(circular_buffer)  # \u8f93\u51fa: [2, 3, 4]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h2>\u4e94\u3001\u6027\u80fd\u6bd4\u8f83<\/h2>\n<\/p>\n<p><h3>\u5217\u8868\u4e58\u6cd5 vs numpy\u6570\u7ec4 vs deque<\/h3>\n<\/p>\n<p><p>\u5728\u9009\u62e9\u5b9a\u957f\u5217\u8868\u7684\u5b9e\u73b0\u65b9\u6cd5\u65f6\uff0c\u6027\u80fd\u662f\u4e00\u4e2a\u91cd\u8981\u7684\u8003\u8651\u56e0\u7d20\u3002\u4ee5\u4e0b\u662f\u5217\u8868\u4e58\u6cd5\u3001numpy\u6570\u7ec4\u548cdeque\u5728\u4e0d\u540c\u64cd\u4f5c\u4e0a\u7684\u6027\u80fd\u6bd4\u8f83\uff1a<\/p>\n<\/p>\n<p><h4>\u521b\u5efa\u5217\u8868<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">import timeit<\/p>\n<h2><strong>\u5217\u8868\u4e58\u6cd5<\/strong><\/h2>\n<p>timeit.timeit(&#39;[0] * 1000&#39;, number=10000)<\/p>\n<h2><strong>numpy\u6570\u7ec4<\/strong><\/h2>\n<p>timeit.timeit(&#39;np.zeros(1000)&#39;, setup=&#39;import numpy as np&#39;, number=10000)<\/p>\n<h2><strong>deque<\/strong><\/h2>\n<p>timeit.timeit(&#39;deque(maxlen=1000)&#39;, setup=&#39;from collections import deque&#39;, number=10000)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>\u4fee\u6539\u5143\u7d20<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\"># \u5217\u8868\u4e58\u6cd5<\/p>\n<p>lst = [0] * 1000<\/p>\n<p>timeit.timeit(&#39;lst[500] = 1&#39;, setup=&#39;lst = [0] * 1000&#39;, number=10000)<\/p>\n<h2><strong>numpy\u6570\u7ec4<\/strong><\/h2>\n<p>arr = np.zeros(1000)<\/p>\n<p>timeit.timeit(&#39;arr[500] = 1&#39;, setup=&#39;import numpy as np; arr = np.zeros(1000)&#39;, number=10000)<\/p>\n<h2><strong>deque<\/strong><\/h2>\n<p>dq = deque([0] * 1000, maxlen=1000)<\/p>\n<p>timeit.timeit(&#39;dq[500] = 1&#39;, setup=&#39;from collections import deque; dq = deque([0] * 1000, maxlen=1000)&#39;, number=10000)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>\u6dfb\u52a0\u5143\u7d20<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\"># \u5217\u8868\u4e58\u6cd5<\/p>\n<p>lst = [0] * 999<\/p>\n<p>timeit.timeit(&#39;lst.append(1)&#39;, setup=&#39;lst = [0] * 999&#39;, number=10000)<\/p>\n<h2><strong>numpy\u6570\u7ec4\uff08\u9700\u8981\u91cd\u65b0\u5206\u914d\u5185\u5b58\uff09<\/strong><\/h2>\n<p>arr = np.zeros(999)<\/p>\n<p>timeit.timeit(&#39;np.append(arr, 1)&#39;, setup=&#39;import numpy as np; arr = np.zeros(999)&#39;, number=10000)<\/p>\n<h2><strong>deque<\/strong><\/h2>\n<p>dq = deque([0] * 999, maxlen=1000)<\/p>\n<p>timeit.timeit(&#39;dq.append(1)&#39;, setup=&#39;from collections import deque; dq = deque([0] * 999, maxlen=1000)&#39;, number=10000)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u901a\u8fc7\u8fd9\u4e9b\u6027\u80fd\u6bd4\u8f83\uff0c\u6211\u4eec\u53ef\u4ee5\u770b\u5230\u5728\u4e0d\u540c\u7684\u64cd\u4f5c\u4e2d\uff0c\u5217\u8868\u4e58\u6cd5\u3001numpy\u6570\u7ec4\u548cdeque\u5404\u6709\u4f18\u52a3\u3002\u5728\u521b\u5efa\u548c\u4fee\u6539\u5143\u7d20\u7684\u64cd\u4f5c\u4e2d\uff0cnumpy\u6570\u7ec4\u901a\u5e38\u8868\u73b0\u66f4\u597d\uff0c\u800c\u5728\u6dfb\u52a0\u5143\u7d20\u7684\u64cd\u4f5c\u4e2d\uff0cdeque\u66f4\u5177\u4f18\u52bf\u3002<\/p>\n<\/p>\n<p><h2>\u516d\u3001\u603b\u7ed3<\/h2>\n<\/p>\n<p><p>Python\u5b9a\u4e49\u5b9a\u957f\u5217\u8868\u7684\u65b9\u6cd5\u6709\u591a\u79cd\uff0c\u5305\u62ec\u5217\u8868\u4e58\u6cd5\u3001numpy\u5e93\u548ccollections.deque\u3002\u6bcf\u79cd\u65b9\u6cd5\u90fd\u6709\u5176\u72ec\u7279\u7684\u7279\u70b9\u548c\u9002\u7528\u573a\u666f\u3002\u5217\u8868\u4e58\u6cd5\u9002\u5408\u521b\u5efa\u7b80\u5355\u7684\u5b9a\u957f\u5217\u8868\uff0cnumpy\u5e93\u9002\u5408\u8fdb\u884c\u9ad8\u6548\u7684\u6570\u503c\u8ba1\u7b97\uff0c\u800ccollections.deque\u9002\u5408\u5b9e\u73b0\u961f\u5217\u3001\u6ed1\u52a8\u7a97\u53e3\u548c\u73af\u5f62\u7f13\u51b2\u533a\u7b49\u6570\u636e\u7ed3\u6784\u3002\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u6839\u636e\u5177\u4f53\u9700\u6c42\u9009\u62e9\u5408\u9002\u7684\u65b9\u6cd5\uff0c\u53ef\u4ee5\u63d0\u9ad8\u4ee3\u7801\u7684\u6548\u7387\u548c\u53ef\u8bfb\u6027\u3002\u901a\u8fc7\u672c\u6587\u7684\u4ecb\u7ecd\uff0c\u5e0c\u671b\u8bfb\u8005\u80fd\u591f\u66f4\u597d\u5730\u7406\u89e3\u548c\u5e94\u7528\u5b9a\u957f\u5217\u8868\u7684\u5404\u79cd\u5b9e\u73b0\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u521b\u5efa\u4e00\u4e2a\u56fa\u5b9a\u957f\u5ea6\u7684\u5217\u8868\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u901a\u8fc7\u591a\u79cd\u65b9\u6cd5\u521b\u5efa\u4e00\u4e2a\u56fa\u5b9a\u957f\u5ea6\u7684\u5217\u8868\u3002\u6700\u5e38\u89c1\u7684\u65b9\u5f0f\u662f\u4f7f\u7528\u5217\u8868\u63a8\u5bfc\u5f0f\u6216\u8005\u76f4\u63a5\u4e58\u6cd5\u8fd0\u7b97\u3002\u4f8b\u5982\uff0c\u53ef\u4ee5\u4f7f\u7528<code>[0] * 10<\/code>\u6765\u521b\u5efa\u4e00\u4e2a\u957f\u5ea6\u4e3a10\u7684\u5217\u8868\uff0c\u6240\u6709\u5143\u7d20\u521d\u59cb\u4e3a0\u3002\u4e5f\u53ef\u4ee5\u4f7f\u7528\u5217\u8868\u63a8\u5bfc\u5f0f\uff0c\u5982<code>[None for _ in range(10)]<\/code>\u6765\u521b\u5efa\u4e00\u4e2a\u5305\u542b10\u4e2aNone\u7684\u5217\u8868\u3002<\/p>\n<p><strong>\u5728Python\u4e2d\uff0c\u56fa\u5b9a\u957f\u5ea6\u7684\u5217\u8868\u53ef\u4ee5\u4fee\u6539\u5417\uff1f<\/strong><br \/>\u662f\u7684\uff0c\u5c3d\u7ba1\u5217\u8868\u7684\u957f\u5ea6\u662f\u56fa\u5b9a\u7684\uff0c\u4f46\u5217\u8868\u4e2d\u7684\u5143\u7d20\u662f\u53ef\u4ee5\u4fee\u6539\u7684\u3002\u60a8\u53ef\u4ee5\u901a\u8fc7\u7d22\u5f15\u8bbf\u95ee\u7279\u5b9a\u5143\u7d20\u5e76\u5bf9\u5176\u8fdb\u884c\u8d4b\u503c\u3002\u4f8b\u5982\uff0c\u5982\u679c\u521b\u5efa\u4e86\u4e00\u4e2a\u957f\u5ea6\u4e3a10\u7684\u5217\u8868\uff0c\u53ef\u4ee5\u901a\u8fc7<code>my_list[0] = 5<\/code>\u5c06\u7b2c\u4e00\u4e2a\u5143\u7d20\u6539\u4e3a5\u3002\u8fd9\u6837\u53ef\u4ee5\u7075\u6d3b\u5730\u7ba1\u7406\u56fa\u5b9a\u957f\u5ea6\u5217\u8868\u4e2d\u7684\u6570\u636e\u3002<\/p>\n<p><strong>Python\u4e2d\u662f\u5426\u6709\u5185\u7f6e\u7684\u6570\u636e\u7ed3\u6784\u53ef\u4ee5\u66ff\u4ee3\u56fa\u5b9a\u957f\u5ea6\u7684\u5217\u8868\uff1f<\/strong><br \/>Python\u7684<code>array<\/code>\u6a21\u5757\u63d0\u4f9b\u4e86\u4e00\u79cd\u6570\u7ec4\u7c7b\u578b\uff0c\u53ef\u4ee5\u7528\u6765\u521b\u5efa\u5177\u6709\u56fa\u5b9a\u7c7b\u578b\u7684\u56fa\u5b9a\u957f\u5ea6\u6570\u7ec4\u3002\u4e0e\u5217\u8868\u76f8\u6bd4\uff0c\u6570\u7ec4\u5728\u5b58\u50a8\u548c\u6027\u80fd\u4e0a\u66f4\u9ad8\u6548\uff0c\u7279\u522b\u662f\u5728\u5904\u7406\u5927\u91cf\u6570\u5b57\u65f6\u3002\u6b64\u5916\uff0c\u4f7f\u7528NumPy\u5e93\u4e5f\u53ef\u4ee5\u521b\u5efa\u56fa\u5b9a\u957f\u5ea6\u7684\u6570\u7ec4\uff0cNumPy\u6570\u7ec4\u5728\u79d1\u5b66\u8ba1\u7b97\u548c\u6570\u636e\u5206\u6790\u4e2d\u975e\u5e38\u6d41\u884c\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"Python\u5b9a\u4e49\u5b9a\u957f\u7684list\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u51e0\u79cd\u65b9\u6cd5\uff1a\u4f7f\u7528\u5217\u8868\u4e58\u6cd5\u3001\u4f7f\u7528numpy\u5e93\u3001\u4f7f\u7528collections [&hellip;]","protected":false},"author":3,"featured_media":1173843,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[37],"tags":[],"acf":[],"_links":{"self":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1173829"}],"collection":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/comments?post=1173829"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1173829\/revisions"}],"predecessor-version":[{"id":1173848,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1173829\/revisions\/1173848"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1173843"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1173829"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1173829"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1173829"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}