{"id":1188439,"date":"2025-01-15T20:17:16","date_gmt":"2025-01-15T12:17:16","guid":{"rendered":""},"modified":"2025-01-15T20:17:19","modified_gmt":"2025-01-15T12:17:19","slug":"python%e5%a6%82%e4%bd%95%e5%8f%96%e6%95%b0%e6%8d%ae%e7%bb%93%e6%9e%84","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1188439.html","title":{"rendered":"python\u5982\u4f55\u53d6\u6570\u636e\u7ed3\u6784"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25140746\/d8e56773-98c4-4dea-9882-df8157a1ed96.webp\" alt=\"python\u5982\u4f55\u53d6\u6570\u636e\u7ed3\u6784\" \/><\/p>\n<p><p> <strong>Python\u53d6\u6570\u636e\u7ed3\u6784\u7684\u65b9\u6cd5<\/strong>\u6709\u5f88\u591a\uff0c\u4e3b\u8981\u5305\u62ec<strong>\u5217\u8868\u3001\u5b57\u5178\u3001\u96c6\u5408\u3001\u5143\u7ec4\u3001\u5b57\u7b26\u4e32\u3001\u961f\u5217\u548c\u5806\u6808\u7b49<\/strong>\u3002\u8fd9\u4e9b\u6570\u636e\u7ed3\u6784\u5404\u6709\u5176\u7279\u70b9\u548c\u9002\u7528\u573a\u666f\uff0c\u4f8b\u5982\uff0c<strong>\u5217\u8868<\/strong>\u662f\u6709\u5e8f\u7684\u3001\u53ef\u53d8\u7684\u6570\u7ec4\uff0c\u9002\u7528\u4e8e\u9700\u8981\u9891\u7e41\u63d2\u5165\u548c\u5220\u9664\u64cd\u4f5c\u7684\u573a\u666f\uff1b<strong>\u5b57\u5178<\/strong>\u662f\u65e0\u5e8f\u7684\u3001\u952e\u503c\u5bf9\u7684\u96c6\u5408\uff0c\u9002\u7528\u4e8e\u9700\u8981\u5feb\u901f\u67e5\u627e\u7684\u573a\u666f\u3002\u63a5\u4e0b\u6765\uff0c\u6211\u4eec\u5c06\u8be6\u7ec6\u63a2\u8ba8Python\u4e2d\u5e38\u7528\u7684\u6570\u636e\u7ed3\u6784\u53ca\u5176\u4f7f\u7528\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u5217\u8868\uff08List\uff09<\/h3>\n<\/p>\n<p><p>\u5217\u8868\u662fPython\u4e2d\u6700\u5e38\u7528\u7684\u6570\u636e\u7ed3\u6784\u4e4b\u4e00\uff0c\u662f\u4e00\u4e2a\u6709\u5e8f\u7684\u3001\u53ef\u53d8\u7684\u5e8f\u5217\uff0c\u652f\u6301\u7d22\u5f15\u3001\u5207\u7247\u548c\u591a\u79cd\u5e38\u7528\u7684\u64cd\u4f5c\u3002<\/p>\n<\/p>\n<p><h4>1. \u521b\u5efa\u5217\u8868<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\"># \u521b\u5efa\u4e00\u4e2a\u7a7a\u5217\u8868<\/p>\n<p>empty_list = []<\/p>\n<h2><strong>\u521b\u5efa\u4e00\u4e2a\u5305\u542b\u5143\u7d20\u7684\u5217\u8868<\/strong><\/h2>\n<p>fruits = [&quot;apple&quot;, &quot;banana&quot;, &quot;cherry&quot;]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2. \u8bbf\u95ee\u5217\u8868\u5143\u7d20<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\"># \u901a\u8fc7\u7d22\u5f15\u8bbf\u95ee\u5217\u8868\u5143\u7d20<\/p>\n<p>first_fruit = fruits[0]  # &#39;apple&#39;<\/p>\n<h2><strong>\u901a\u8fc7\u8d1f\u7d22\u5f15\u8bbf\u95ee\u5217\u8868\u5143\u7d20<\/strong><\/h2>\n<p>last_fruit = fruits[-1]  # &#39;cherry&#39;<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>3. \u4fee\u6539\u5217\u8868\u5143\u7d20<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\"># \u4fee\u6539\u6307\u5b9a\u7d22\u5f15\u5904\u7684\u5143\u7d20<\/p>\n<p>fruits[1] = &quot;blueberry&quot;<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>4. \u5217\u8868\u7684\u5e38\u7528\u64cd\u4f5c<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\"># \u6dfb\u52a0\u5143\u7d20\u5230\u5217\u8868\u672b\u5c3e<\/p>\n<p>fruits.append(&quot;date&quot;)<\/p>\n<h2><strong>\u63d2\u5165\u5143\u7d20\u5230\u6307\u5b9a\u4f4d\u7f6e<\/strong><\/h2>\n<p>fruits.insert(1, &quot;banana&quot;)<\/p>\n<h2><strong>\u5220\u9664\u6307\u5b9a\u4f4d\u7f6e\u7684\u5143\u7d20<\/strong><\/h2>\n<p>del fruits[2]<\/p>\n<h2><strong>\u5220\u9664\u6307\u5b9a\u5143\u7d20<\/strong><\/h2>\n<p>fruits.remove(&quot;banana&quot;)<\/p>\n<h2><strong>\u83b7\u53d6\u5217\u8868\u957f\u5ea6<\/strong><\/h2>\n<p>length = len(fruits)<\/p>\n<h2><strong>\u5217\u8868\u62fc\u63a5<\/strong><\/h2>\n<p>combined_list = fruits + [&quot;elderberry&quot;, &quot;fig&quot;]<\/p>\n<h2><strong>\u5217\u8868\u5207\u7247<\/strong><\/h2>\n<p>subset = fruits[1:3]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e8c\u3001\u5b57\u5178\uff08Dictionary\uff09<\/h3>\n<\/p>\n<p><p>\u5b57\u5178\u662f\u53e6\u4e00\u79cd\u5e38\u7528\u7684\u6570\u636e\u7ed3\u6784\uff0c\u4ee5\u952e\u503c\u5bf9\u7684\u5f62\u5f0f\u5b58\u50a8\u6570\u636e\uff0c\u952e\u662f\u552f\u4e00\u7684\uff0c\u503c\u53ef\u4ee5\u662f\u4efb\u610f\u7c7b\u578b\u3002<\/p>\n<\/p>\n<p><h4>1. \u521b\u5efa\u5b57\u5178<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\"># \u521b\u5efa\u4e00\u4e2a\u7a7a\u5b57\u5178<\/p>\n<p>empty_dict = {}<\/p>\n<h2><strong>\u521b\u5efa\u4e00\u4e2a\u5305\u542b\u952e\u503c\u5bf9\u7684\u5b57\u5178<\/strong><\/h2>\n<p>person = {&quot;name&quot;: &quot;John&quot;, &quot;age&quot;: 30, &quot;city&quot;: &quot;New York&quot;}<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2. \u8bbf\u95ee\u5b57\u5178\u5143\u7d20<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\"># \u901a\u8fc7\u952e\u8bbf\u95ee\u5b57\u5178\u5143\u7d20<\/p>\n<p>name = person[&quot;name&quot;]  # &#39;John&#39;<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>3. \u4fee\u6539\u5b57\u5178\u5143\u7d20<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\"># \u4fee\u6539\u6307\u5b9a\u952e\u7684\u503c<\/p>\n<p>person[&quot;age&quot;] = 31<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>4. \u5b57\u5178\u7684\u5e38\u7528\u64cd\u4f5c<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\"># \u6dfb\u52a0\u65b0\u7684\u952e\u503c\u5bf9<\/p>\n<p>person[&quot;em<a href=\"https:\/\/docs.pingcode.com\/blog\/59162.html\" target=\"_blank\">AI<\/a>l&quot;] = &quot;john@example.com&quot;<\/p>\n<h2><strong>\u5220\u9664\u6307\u5b9a\u952e\u503c\u5bf9<\/strong><\/h2>\n<p>del person[&quot;city&quot;]<\/p>\n<h2><strong>\u83b7\u53d6\u5b57\u5178\u6240\u6709\u7684\u952e<\/strong><\/h2>\n<p>keys = person.keys()<\/p>\n<h2><strong>\u83b7\u53d6\u5b57\u5178\u6240\u6709\u7684\u503c<\/strong><\/h2>\n<p>values = person.values()<\/p>\n<h2><strong>\u83b7\u53d6\u5b57\u5178\u6240\u6709\u7684\u952e\u503c\u5bf9<\/strong><\/h2>\n<p>items = person.items()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e09\u3001\u96c6\u5408\uff08Set\uff09<\/h3>\n<\/p>\n<p><p>\u96c6\u5408\u662f\u4e00\u4e2a\u65e0\u5e8f\u7684\u3001\u4e0d\u91cd\u590d\u7684\u5143\u7d20\u96c6\u5408\uff0c\u4e3b\u8981\u7528\u4e8e\u6210\u5458\u5173\u7cfb\u6d4b\u8bd5\u548c\u6d88\u9664\u91cd\u590d\u5143\u7d20\u3002<\/p>\n<\/p>\n<p><h4>1. \u521b\u5efa\u96c6\u5408<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\"># \u521b\u5efa\u4e00\u4e2a\u7a7a\u96c6\u5408<\/p>\n<p>empty_set = set()<\/p>\n<h2><strong>\u521b\u5efa\u4e00\u4e2a\u5305\u542b\u5143\u7d20\u7684\u96c6\u5408<\/strong><\/h2>\n<p>fruits_set = {&quot;apple&quot;, &quot;banana&quot;, &quot;cherry&quot;}<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2. \u96c6\u5408\u7684\u5e38\u7528\u64cd\u4f5c<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\"># \u6dfb\u52a0\u5143\u7d20\u5230\u96c6\u5408<\/p>\n<p>fruits_set.add(&quot;date&quot;)<\/p>\n<h2><strong>\u5220\u9664\u6307\u5b9a\u5143\u7d20<\/strong><\/h2>\n<p>fruits_set.remove(&quot;banana&quot;)<\/p>\n<h2><strong>\u83b7\u53d6\u96c6\u5408\u957f\u5ea6<\/strong><\/h2>\n<p>length = len(fruits_set)<\/p>\n<h2><strong>\u96c6\u5408\u7684\u96c6\u5408\u64cd\u4f5c<\/strong><\/h2>\n<p>another_set = {&quot;cherry&quot;, &quot;date&quot;, &quot;elderberry&quot;}<\/p>\n<h2><strong>\u5e76\u96c6<\/strong><\/h2>\n<p>union_set = fruits_set | another_set<\/p>\n<h2><strong>\u4ea4\u96c6<\/strong><\/h2>\n<p>intersection_set = fruits_set &amp; another_set<\/p>\n<h2><strong>\u5dee\u96c6<\/strong><\/h2>\n<p>difference_set = fruits_set - another_set<\/p>\n<h2><strong>\u5bf9\u79f0\u5dee\u96c6<\/strong><\/h2>\n<p>symmetric_difference_set = fruits_set ^ another_set<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u56db\u3001\u5143\u7ec4\uff08Tuple\uff09<\/h3>\n<\/p>\n<p><p>\u5143\u7ec4\u662f\u4e00\u4e2a\u6709\u5e8f\u7684\u3001\u4e0d\u53ef\u53d8\u7684\u5e8f\u5217\uff0c\u4e0e\u5217\u8868\u7c7b\u4f3c\uff0c\u4f46\u5143\u7ec4\u4e00\u65e6\u521b\u5efa\u5c31\u4e0d\u80fd\u4fee\u6539\u3002<\/p>\n<\/p>\n<p><h4>1. \u521b\u5efa\u5143\u7ec4<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\"># \u521b\u5efa\u4e00\u4e2a\u7a7a\u5143\u7ec4<\/p>\n<p>empty_tuple = ()<\/p>\n<h2><strong>\u521b\u5efa\u4e00\u4e2a\u5305\u542b\u5143\u7d20\u7684\u5143\u7ec4<\/strong><\/h2>\n<p>fruits_tuple = (&quot;apple&quot;, &quot;banana&quot;, &quot;cherry&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2. \u8bbf\u95ee\u5143\u7ec4\u5143\u7d20<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\"># \u901a\u8fc7\u7d22\u5f15\u8bbf\u95ee\u5143\u7ec4\u5143\u7d20<\/p>\n<p>first_fruit = fruits_tuple[0]  # &#39;apple&#39;<\/p>\n<h2><strong>\u901a\u8fc7\u8d1f\u7d22\u5f15\u8bbf\u95ee\u5143\u7ec4\u5143\u7d20<\/strong><\/h2>\n<p>last_fruit = fruits_tuple[-1]  # &#39;cherry&#39;<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>3. \u5143\u7ec4\u7684\u5e38\u7528\u64cd\u4f5c<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\"># \u83b7\u53d6\u5143\u7ec4\u957f\u5ea6<\/p>\n<p>length = len(fruits_tuple)<\/p>\n<h2><strong>\u5143\u7ec4\u62fc\u63a5<\/strong><\/h2>\n<p>combined_tuple = fruits_tuple + (&quot;date&quot;, &quot;elderberry&quot;)<\/p>\n<h2><strong>\u5143\u7ec4\u5207\u7247<\/strong><\/h2>\n<p>subset = fruits_tuple[1:3]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e94\u3001\u5b57\u7b26\u4e32\uff08String\uff09<\/h3>\n<\/p>\n<p><p>\u5b57\u7b26\u4e32\u662f\u4e00\u4e2a\u6709\u5e8f\u7684\u5b57\u7b26\u5e8f\u5217\uff0c\u867d\u7136\u770b\u8d77\u6765\u4e0e\u5176\u4ed6\u6570\u636e\u7ed3\u6784\u6709\u76f8\u4f3c\u4e4b\u5904\uff0c\u4f46\u5b57\u7b26\u4e32\u662f\u4e0d\u53ef\u53d8\u7684\u3002<\/p>\n<\/p>\n<p><h4>1. \u521b\u5efa\u5b57\u7b26\u4e32<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\"># \u521b\u5efa\u4e00\u4e2a\u5b57\u7b26\u4e32<\/p>\n<p>greeting = &quot;Hello, world!&quot;<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2. \u8bbf\u95ee\u5b57\u7b26\u4e32\u5143\u7d20<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\"># \u901a\u8fc7\u7d22\u5f15\u8bbf\u95ee\u5b57\u7b26\u4e32\u5143\u7d20<\/p>\n<p>first_char = greeting[0]  # &#39;H&#39;<\/p>\n<h2><strong>\u901a\u8fc7\u8d1f\u7d22\u5f15\u8bbf\u95ee\u5b57\u7b26\u4e32\u5143\u7d20<\/strong><\/h2>\n<p>last_char = greeting[-1]  # &#39;!&#39;<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>3. \u5b57\u7b26\u4e32\u7684\u5e38\u7528\u64cd\u4f5c<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\"># \u83b7\u53d6\u5b57\u7b26\u4e32\u957f\u5ea6<\/p>\n<p>length = len(greeting)<\/p>\n<h2><strong>\u5b57\u7b26\u4e32\u62fc\u63a5<\/strong><\/h2>\n<p>combined_string = greeting + &quot; How are you?&quot;<\/p>\n<h2><strong>\u5b57\u7b26\u4e32\u5207\u7247<\/strong><\/h2>\n<p>subset = greeting[7:12]<\/p>\n<h2><strong>\u5b57\u7b26\u4e32\u8f6c\u4e3a\u5927\u5199<\/strong><\/h2>\n<p>uppercase_string = greeting.upper()<\/p>\n<h2><strong>\u5b57\u7b26\u4e32\u8f6c\u4e3a\u5c0f\u5199<\/strong><\/h2>\n<p>lowercase_string = greeting.lower()<\/p>\n<h2><strong>\u66ff\u6362\u5b57\u7b26\u4e32\u4e2d\u7684\u90e8\u5206\u5185\u5bb9<\/strong><\/h2>\n<p>new_greeting = greeting.replace(&quot;world&quot;, &quot;Python&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u516d\u3001\u961f\u5217\uff08Queue\uff09<\/h3>\n<\/p>\n<p><p>\u961f\u5217\u662f\u4e00\u79cd\u5148\u8fdb\u5148\u51fa\uff08FIFO\uff09\u7684\u6570\u636e\u7ed3\u6784\uff0c\u5e38\u7528\u4e8e\u9700\u8981\u6309\u987a\u5e8f\u5904\u7406\u5143\u7d20\u7684\u573a\u666f\u3002Python\u4e2d\u53ef\u4ee5\u4f7f\u7528<code>collections.deque<\/code>\u6765\u5b9e\u73b0\u961f\u5217\u3002<\/p>\n<\/p>\n<p><h4>1. \u521b\u5efa\u961f\u5217<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">from collections import deque<\/p>\n<h2><strong>\u521b\u5efa\u4e00\u4e2a\u7a7a\u961f\u5217<\/strong><\/h2>\n<p>queue = deque()<\/p>\n<h2><strong>\u521b\u5efa\u4e00\u4e2a\u5305\u542b\u5143\u7d20\u7684\u961f\u5217<\/strong><\/h2>\n<p>queue = deque([&quot;apple&quot;, &quot;banana&quot;, &quot;cherry&quot;])<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2. \u961f\u5217\u7684\u5e38\u7528\u64cd\u4f5c<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\"># \u5411\u961f\u5217\u5c3e\u90e8\u6dfb\u52a0\u5143\u7d20<\/p>\n<p>queue.append(&quot;date&quot;)<\/p>\n<h2><strong>\u4ece\u961f\u5217\u5934\u90e8\u79fb\u9664\u5143\u7d20<\/strong><\/h2>\n<p>first_item = queue.popleft()<\/p>\n<h2><strong>\u83b7\u53d6\u961f\u5217\u957f\u5ea6<\/strong><\/h2>\n<p>length = len(queue)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e03\u3001\u5806\u6808\uff08Stack\uff09<\/h3>\n<\/p>\n<p><p>\u5806\u6808\u662f\u4e00\u79cd\u540e\u8fdb\u5148\u51fa\uff08LIFO\uff09\u7684\u6570\u636e\u7ed3\u6784\uff0c\u5e38\u7528\u4e8e\u9700\u8981\u6309\u9006\u5e8f\u5904\u7406\u5143\u7d20\u7684\u573a\u666f\u3002Python\u4e2d\u53ef\u4ee5\u4f7f\u7528\u5217\u8868\u6765\u5b9e\u73b0\u5806\u6808\u3002<\/p>\n<\/p>\n<p><h4>1. \u521b\u5efa\u5806\u6808<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\"># \u521b\u5efa\u4e00\u4e2a\u7a7a\u5806\u6808<\/p>\n<p>stack = []<\/p>\n<h2><strong>\u521b\u5efa\u4e00\u4e2a\u5305\u542b\u5143\u7d20\u7684\u5806\u6808<\/strong><\/h2>\n<p>stack = [&quot;apple&quot;, &quot;banana&quot;, &quot;cherry&quot;]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2. \u5806\u6808\u7684\u5e38\u7528\u64cd\u4f5c<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\"># \u5411\u5806\u6808\u9876\u90e8\u6dfb\u52a0\u5143\u7d20<\/p>\n<p>stack.append(&quot;date&quot;)<\/p>\n<h2><strong>\u4ece\u5806\u6808\u9876\u90e8\u79fb\u9664\u5143\u7d20<\/strong><\/h2>\n<p>top_item = stack.pop()<\/p>\n<h2><strong>\u83b7\u53d6\u5806\u6808\u957f\u5ea6<\/strong><\/h2>\n<p>length = len(stack)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u516b\u3001\u5176\u4ed6\u6570\u636e\u7ed3\u6784<\/h3>\n<\/p>\n<p><p>\u9664\u4e86\u4e0a\u8ff0\u5e38\u7528\u7684\u6570\u636e\u7ed3\u6784\u5916\uff0cPython\u8fd8\u63d0\u4f9b\u4e86\u4e00\u4e9b\u9ad8\u7ea7\u6570\u636e\u7ed3\u6784\uff0c\u5982<code>collections.namedtuple<\/code>\u3001<code>collections.OrderedDict<\/code>\u3001<code>collections.defaultdict<\/code>\u3001<code>heapq<\/code>\u7b49\u3002<\/p>\n<\/p>\n<p><h4>1. namedtuple<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">from collections import namedtuple<\/p>\n<h2><strong>\u521b\u5efa\u4e00\u4e2a\u547d\u540d\u5143\u7ec4\u7c7b\u578b<\/strong><\/h2>\n<p>Point = namedtuple(&quot;Point&quot;, [&quot;x&quot;, &quot;y&quot;])<\/p>\n<h2><strong>\u521b\u5efa\u4e00\u4e2a\u547d\u540d\u5143\u7ec4\u5b9e\u4f8b<\/strong><\/h2>\n<p>p = Point(1, 2)<\/p>\n<h2><strong>\u8bbf\u95ee\u547d\u540d\u5143\u7ec4\u5143\u7d20<\/strong><\/h2>\n<p>x = p.x  # 1<\/p>\n<p>y = p.y  # 2<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2. OrderedDict<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">from collections import OrderedDict<\/p>\n<h2><strong>\u521b\u5efa\u4e00\u4e2a\u6709\u5e8f\u5b57\u5178<\/strong><\/h2>\n<p>ordered_dict = OrderedDict()<\/p>\n<h2><strong>\u6dfb\u52a0\u952e\u503c\u5bf9<\/strong><\/h2>\n<p>ordered_dict[&quot;a&quot;] = 1<\/p>\n<p>ordered_dict[&quot;b&quot;] = 2<\/p>\n<p>ordered_dict[&quot;c&quot;] = 3<\/p>\n<h2><strong>\u8bbf\u95ee\u6709\u5e8f\u5b57\u5178\u5143\u7d20<\/strong><\/h2>\n<p>for key, value in ordered_dict.items():<\/p>\n<p>    print(key, value)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>3. defaultdict<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">from collections import defaultdict<\/p>\n<h2><strong>\u521b\u5efa\u4e00\u4e2a\u5e26\u6709\u9ed8\u8ba4\u503c\u7684\u5b57\u5178<\/strong><\/h2>\n<p>default_dict = defaultdict(int)<\/p>\n<h2><strong>\u8bbf\u95ee\u5b57\u5178\u5143\u7d20<\/strong><\/h2>\n<p>default_dict[&quot;a&quot;] += 1  # \u9ed8\u8ba4\u503c\u4e3a0\uff0c\u6240\u4ee5\u7ed3\u679c\u4e3a1<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>4. heapq<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">import heapq<\/p>\n<h2><strong>\u521b\u5efa\u4e00\u4e2a\u7a7a\u5806<\/strong><\/h2>\n<p>heap = []<\/p>\n<h2><strong>\u5411\u5806\u4e2d\u6dfb\u52a0\u5143\u7d20<\/strong><\/h2>\n<p>heapq.heappush(heap, 3)<\/p>\n<p>heapq.heappush(heap, 1)<\/p>\n<p>heapq.heappush(heap, 4)<\/p>\n<h2><strong>\u4ece\u5806\u4e2d\u79fb\u9664\u6700\u5c0f\u5143\u7d20<\/strong><\/h2>\n<p>smallest = heapq.heappop(heap)  # 1<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e5d\u3001\u603b\u7ed3<\/h3>\n<\/p>\n<p><p>\u901a\u8fc7\u4ee5\u4e0a\u5bf9Python\u4e2d\u5404\u79cd\u5e38\u7528\u6570\u636e\u7ed3\u6784\u7684\u4ecb\u7ecd\uff0c\u6211\u4eec\u53ef\u4ee5\u770b\u5230\uff0c\u6bcf\u79cd\u6570\u636e\u7ed3\u6784\u90fd\u6709\u5176\u72ec\u7279\u7684\u7279\u6027\u548c\u9002\u7528\u573a\u666f\u3002<strong>\u5217\u8868<\/strong>\u9002\u7528\u4e8e\u9700\u8981\u9891\u7e41\u63d2\u5165\u548c\u5220\u9664\u64cd\u4f5c\u7684\u573a\u666f\uff0c<strong>\u5b57\u5178<\/strong>\u9002\u7528\u4e8e\u9700\u8981\u5feb\u901f\u67e5\u627e\u7684\u573a\u666f\uff0c<strong>\u96c6\u5408<\/strong>\u9002\u7528\u4e8e\u6210\u5458\u5173\u7cfb\u6d4b\u8bd5\u548c\u6d88\u9664\u91cd\u590d\u5143\u7d20\u7684\u573a\u666f\uff0c<strong>\u5143\u7ec4<\/strong>\u9002\u7528\u4e8e\u9700\u8981\u4e0d\u53ef\u53d8\u5e8f\u5217\u7684\u573a\u666f\uff0c<strong>\u5b57\u7b26\u4e32<\/strong>\u9002\u7528\u4e8e\u6587\u672c\u5904\u7406\uff0c<strong>\u961f\u5217<\/strong>\u548c<strong>\u5806\u6808<\/strong>\u5206\u522b\u9002\u7528\u4e8e\u6309\u987a\u5e8f\u548c\u9006\u5e8f\u5904\u7406\u5143\u7d20\u7684\u573a\u666f\u3002\u638c\u63e1\u8fd9\u4e9b\u6570\u636e\u7ed3\u6784\u7684\u4f7f\u7528\u65b9\u6cd5\uff0c\u5bf9\u4e8e\u7f16\u5199\u9ad8\u6548\u3001\u7b80\u6d01\u7684Python\u4ee3\u7801\u81f3\u5173\u91cd\u8981\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u9009\u62e9\u5408\u9002\u7684\u6570\u636e\u7ed3\u6784\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u9009\u62e9\u6570\u636e\u7ed3\u6784\u901a\u5e38\u53d6\u51b3\u4e8e\u5177\u4f53\u7684\u5e94\u7528\u573a\u666f\u548c\u9700\u6c42\u3002\u5e38\u89c1\u7684\u6570\u636e\u7ed3\u6784\u5305\u62ec\u5217\u8868\u3001\u5143\u7ec4\u3001\u5b57\u5178\u548c\u96c6\u5408\u3002\u5217\u8868\u9002\u5408\u9700\u8981\u6709\u5e8f\u5b58\u50a8\u548c\u53ef\u53d8\u7684\u6570\u636e\uff0c\u5143\u7ec4\u9002\u5408\u5b58\u50a8\u4e0d\u9700\u8981\u4fee\u6539\u7684\u6570\u636e\uff0c\u5b57\u5178\u5219\u975e\u5e38\u9002\u5408\u9700\u8981\u901a\u8fc7\u952e\u5feb\u901f\u67e5\u627e\u503c\u7684\u573a\u5408\uff0c\u800c\u96c6\u5408\u5219\u7528\u4e8e\u5904\u7406\u552f\u4e00\u5143\u7d20\u7684\u96c6\u5408\u64cd\u4f5c\u3002\u5206\u6790\u60a8\u7684\u6570\u636e\u7279\u5f81\u548c\u64cd\u4f5c\u9700\u6c42\uff0c\u6709\u52a9\u4e8e\u505a\u51fa\u660e\u667a\u7684\u9009\u62e9\u3002<\/p>\n<p><strong>Python\u6570\u636e\u7ed3\u6784\u7684\u6027\u80fd\u6bd4\u8f83\u662f\u4ec0\u4e48\u6837\u7684\uff1f<\/strong><br \/>\u4e0d\u540c\u7684\u6570\u636e\u7ed3\u6784\u5728\u6027\u80fd\u4e0a\u6709\u5f88\u5927\u5dee\u5f02\u3002\u5217\u8868\u7684\u968f\u673a\u8bbf\u95ee\u65f6\u95f4\u590d\u6742\u5ea6\u4e3aO(1)\uff0c\u4f46\u63d2\u5165\u548c\u5220\u9664\u64cd\u4f5c\u7684\u65f6\u95f4\u590d\u6742\u5ea6\u4e3aO(n)\u3002\u5b57\u5178\u63d0\u4f9bO(1)\u7684\u5e73\u5747\u67e5\u627e\u65f6\u95f4\uff0c\u9002\u5408\u5feb\u901f\u6570\u636e\u8bbf\u95ee\u3002\u96c6\u5408\u540c\u6837\u652f\u6301O(1)\u7684\u67e5\u627e\u6027\u80fd\uff0c\u975e\u5e38\u9002\u5408\u9700\u8981\u9ad8\u6548\u5904\u7406\u91cd\u590d\u6570\u636e\u7684\u60c5\u51b5\u3002\u4e86\u89e3\u8fd9\u4e9b\u6027\u80fd\u7279\u6027\u53ef\u4ee5\u5e2e\u52a9\u60a8\u4f18\u5316\u4ee3\u7801\u7684\u6267\u884c\u6548\u7387\u3002<\/p>\n<p><strong>\u5982\u4f55\u5728Python\u4e2d\u81ea\u5b9a\u4e49\u6570\u636e\u7ed3\u6784\uff1f<\/strong><br \/>\u81ea\u5b9a\u4e49\u6570\u636e\u7ed3\u6784\u901a\u5e38\u901a\u8fc7\u5b9a\u4e49\u7c7b\u6765\u5b9e\u73b0\u3002\u5728\u7c7b\u4e2d\uff0c\u60a8\u53ef\u4ee5\u4f7f\u7528\u5c5e\u6027\u548c\u65b9\u6cd5\u6765\u5c01\u88c5\u6570\u636e\u548c\u76f8\u5173\u64cd\u4f5c\u3002\u4f8b\u5982\uff0c\u53ef\u4ee5\u521b\u5efa\u4e00\u4e2a\u7c7b\u8868\u793a\u6808\u7ed3\u6784\uff0c\u5b9a\u4e49\u5165\u6808\u548c\u51fa\u6808\u7684\u65b9\u6cd5\u3002\u5229\u7528Python\u7684\u7c7b\u548c\u5bf9\u8c61\u7279\u6027\uff0c\u60a8\u53ef\u4ee5\u5b9e\u73b0\u590d\u6742\u7684\u6570\u636e\u7ed3\u6784\uff0c\u6ee1\u8db3\u7279\u5b9a\u7684\u9700\u6c42\u3002\u540c\u65f6\uff0c\u4f7f\u7528\u5185\u7f6e\u7684<code>collections<\/code>\u6a21\u5757\u4e2d\u7684deque\u6216namedtuple\u7b49\u4e5f\u53ef\u4ee5\u5feb\u901f\u6784\u5efa\u81ea\u5b9a\u4e49\u6570\u636e\u7ed3\u6784\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"Python\u53d6\u6570\u636e\u7ed3\u6784\u7684\u65b9\u6cd5\u6709\u5f88\u591a\uff0c\u4e3b\u8981\u5305\u62ec\u5217\u8868\u3001\u5b57\u5178\u3001\u96c6\u5408\u3001\u5143\u7ec4\u3001\u5b57\u7b26\u4e32\u3001\u961f\u5217\u548c\u5806\u6808\u7b49\u3002\u8fd9\u4e9b\u6570\u636e\u7ed3\u6784\u5404\u6709\u5176\u7279 [&hellip;]","protected":false},"author":3,"featured_media":1188443,"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\/1188439"}],"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=1188439"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1188439\/revisions"}],"predecessor-version":[{"id":1188445,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1188439\/revisions\/1188445"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1188443"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1188439"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1188439"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1188439"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}