{"id":1096594,"date":"2025-01-08T15:02:54","date_gmt":"2025-01-08T07:02:54","guid":{"rendered":""},"modified":"2025-01-08T15:02:57","modified_gmt":"2025-01-08T07:02:57","slug":"python%e5%a6%82%e4%bd%95%e8%bf%94%e5%9b%9e%e7%ac%ac%e4%b8%80%e9%83%a8-2","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1096594.html","title":{"rendered":"python\u5982\u4f55\u8fd4\u56de\u7b2c\u4e00\u90e8"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/24211731\/b7b37454-fc02-4412-b220-1be97468b614.webp\" alt=\"python\u5982\u4f55\u8fd4\u56de\u7b2c\u4e00\u90e8\" \/><\/p>\n<p><p> <strong>\u4f7f\u7528 Python \u8fd4\u56de\u7b2c\u4e00\u4e2a\u5143\u7d20\u7684\u65b9\u6cd5\u4e3b\u8981\u6709\uff1a\u4f7f\u7528\u7d22\u5f15\u8bbf\u95ee\u3001\u4f7f\u7528\u89e3\u5305\u3001\u4f7f\u7528\u8fed\u4ee3\u5668\u7b49\u65b9\u6cd5<\/strong>\u3002\u4e0b\u9762\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u5176\u4e2d\u4e00\u79cd\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<p><p>\u5728 Python \u4e2d\uff0c\u6700\u5e38\u89c1\u7684\u65b9\u5f0f\u662f\u901a\u8fc7\u7d22\u5f15\u8bbf\u95ee\u5143\u7d20\u3002\u5bf9\u4e8e\u4e00\u4e2a\u5217\u8868\u3001\u5143\u7ec4\u7b49\u5e8f\u5217\u7c7b\u578b\u7684\u6570\u636e\u7ed3\u6784\uff0c\u53ef\u4ee5\u4f7f\u7528\u7d22\u5f15\u6765\u76f4\u63a5\u8bbf\u95ee\u7b2c\u4e00\u4e2a\u5143\u7d20\u3002\u4f8b\u5982\uff0c\u5bf9\u4e8e\u5217\u8868 <code>my_list<\/code>\uff0c\u53ef\u4ee5\u901a\u8fc7 <code>my_list[0]<\/code> \u6765\u83b7\u53d6\u7b2c\u4e00\u4e2a\u5143\u7d20\u3002\u7d22\u5f15\u4ece0\u5f00\u59cb\uff0c\u8fd9\u610f\u5473\u7740\u7b2c\u4e00\u4e2a\u5143\u7d20\u7684\u7d22\u5f15\u662f0\u3002\u8fd9\u4e2a\u65b9\u6cd5\u7684\u4f18\u70b9\u662f\u7b80\u5355\u3001\u76f4\u89c2\uff0c\u5e76\u4e14\u53ef\u4ee5\u9002\u7528\u4e8e\u5927\u591a\u6570\u5e8f\u5217\u7c7b\u578b\u7684\u6570\u636e\u7ed3\u6784\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u4f7f\u7528\u7d22\u5f15\u8bbf\u95ee<\/h3>\n<\/p>\n<p><p>Python \u63d0\u4f9b\u4e86\u5f3a\u5927\u7684\u7d22\u5f15\u673a\u5236\uff0c\u53ef\u4ee5\u5f88\u65b9\u4fbf\u5730\u83b7\u53d6\u5e8f\u5217\u4e2d\u7684\u67d0\u4e2a\u5143\u7d20\u3002\u8fd9\u91cc\u6211\u4eec\u901a\u8fc7\u7d22\u5f15\u8bbf\u95ee\u6765\u83b7\u53d6\u7b2c\u4e00\u4e2a\u5143\u7d20\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">my_list = [1, 2, 3, 4, 5]<\/p>\n<p>first_element = my_list[0]<\/p>\n<p>print(first_element)  # \u8f93\u51fa\uff1a1<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u8ff0\u4ee3\u7801\u4e2d\uff0c\u901a\u8fc7 <code>my_list[0]<\/code> \u6211\u4eec\u53ef\u4ee5\u83b7\u53d6\u5217\u8868 <code>my_list<\/code> \u7684\u7b2c\u4e00\u4e2a\u5143\u7d20\u3002\u8fd9\u4e2a\u65b9\u6cd5\u4e0d\u4ec5\u9002\u7528\u4e8e\u5217\u8868\uff0c\u4e5f\u9002\u7528\u4e8e\u5143\u7ec4\u3001\u5b57\u7b26\u4e32\u7b49\u5e8f\u5217\u7c7b\u578b\u7684\u6570\u636e\u7ed3\u6784\u3002<\/p>\n<\/p>\n<p><h3>\u4e8c\u3001\u4f7f\u7528\u89e3\u5305<\/h3>\n<\/p>\n<p><p>\u89e3\u5305\uff08unpacking\uff09\u662f Python \u4e2d\u7684\u4e00\u79cd\u8bed\u6cd5\u7cd6\uff0c\u53ef\u4ee5\u5c06\u5e8f\u5217\u7c7b\u578b\u7684\u6570\u636e\u7ed3\u6784\u89e3\u5305\u6210\u591a\u4e2a\u53d8\u91cf\u3002\u53ef\u4ee5\u5229\u7528\u8fd9\u4e00\u7279\u6027\u6765\u83b7\u53d6\u7b2c\u4e00\u4e2a\u5143\u7d20\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">my_list = [1, 2, 3, 4, 5]<\/p>\n<p>first_element, *rest = my_list<\/p>\n<p>print(first_element)  # \u8f93\u51fa\uff1a1<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u8ff0\u4ee3\u7801\u4e2d\uff0c<code>first_element<\/code> \u88ab\u8d4b\u503c\u4e3a <code>my_list<\/code> \u7684\u7b2c\u4e00\u4e2a\u5143\u7d20\uff0c\u800c <code>*rest<\/code> \u5219\u8868\u793a\u5269\u4f59\u7684\u5143\u7d20\u3002\u8fd9\u79cd\u65b9\u6cd5\u4e0d\u4ec5\u53ef\u4ee5\u83b7\u53d6\u7b2c\u4e00\u4e2a\u5143\u7d20\uff0c\u8fd8\u80fd\u540c\u65f6\u83b7\u53d6\u5269\u4f59\u5143\u7d20\u3002<\/p>\n<\/p>\n<p><h3>\u4e09\u3001\u4f7f\u7528\u8fed\u4ee3\u5668<\/h3>\n<\/p>\n<p><p>\u8fed\u4ee3\u5668\u662f\u4e00\u79cd\u8bbf\u95ee\u96c6\u5408\u5143\u7d20\u7684\u65b9\u5f0f\uff0cPython \u63d0\u4f9b\u4e86\u4e30\u5bcc\u7684\u8fed\u4ee3\u5668\u63a5\u53e3\uff0c\u53ef\u4ee5\u901a\u8fc7\u8fed\u4ee3\u5668\u6765\u83b7\u53d6\u7b2c\u4e00\u4e2a\u5143\u7d20\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">my_list = [1, 2, 3, 4, 5]<\/p>\n<p>iterator = iter(my_list)<\/p>\n<p>first_element = next(iterator)<\/p>\n<p>print(first_element)  # \u8f93\u51fa\uff1a1<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u8ff0\u4ee3\u7801\u4e2d\uff0c\u9996\u5148\u901a\u8fc7 <code>iter()<\/code> \u51fd\u6570\u5c06\u5217\u8868 <code>my_list<\/code> \u8f6c\u6362\u4e3a\u8fed\u4ee3\u5668\uff0c\u7136\u540e\u901a\u8fc7 <code>next()<\/code> \u51fd\u6570\u83b7\u53d6\u8fed\u4ee3\u5668\u7684\u7b2c\u4e00\u4e2a\u5143\u7d20\u3002<\/p>\n<\/p>\n<p><h3>\u56db\u3001\u4f7f\u7528\u5207\u7247<\/h3>\n<\/p>\n<p><p>\u5207\u7247\u662f\u4e00\u79cd\u975e\u5e38\u7075\u6d3b\u7684\u64cd\u4f5c\uff0c\u53ef\u4ee5\u7528\u4e8e\u83b7\u53d6\u5e8f\u5217\u4e2d\u7684\u5b50\u5e8f\u5217\u3002\u53ef\u4ee5\u5229\u7528\u5207\u7247\u6765\u83b7\u53d6\u7b2c\u4e00\u4e2a\u5143\u7d20\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">my_list = [1, 2, 3, 4, 5]<\/p>\n<p>first_element = my_list[:1][0]<\/p>\n<p>print(first_element)  # \u8f93\u51fa\uff1a1<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u8ff0\u4ee3\u7801\u4e2d\uff0c<code>my_list[:1]<\/code> \u8868\u793a\u83b7\u53d6\u5217\u8868\u7684\u524d\u4e00\u4e2a\u5143\u7d20\uff0c\u8fd4\u56de\u7684\u662f\u4e00\u4e2a\u5217\u8868\uff0c<code>[0]<\/code> \u5219\u662f\u83b7\u53d6\u8fd9\u4e2a\u5217\u8868\u4e2d\u7684\u7b2c\u4e00\u4e2a\u5143\u7d20\u3002<\/p>\n<\/p>\n<p><h3>\u4e94\u3001\u4f7f\u7528 next \u548c iter \u51fd\u6570<\/h3>\n<\/p>\n<p><p>\u5728 Python \u4e2d\uff0c<code>next()<\/code> \u51fd\u6570\u7528\u4e8e\u8fd4\u56de\u8fed\u4ee3\u5668\u7684\u4e0b\u4e00\u4e2a\u9879\u76ee\uff0c\u800c <code>iter()<\/code> \u51fd\u6570\u7528\u4e8e\u751f\u6210\u4e00\u4e2a\u8fed\u4ee3\u5668\u5bf9\u8c61\u3002\u53ef\u4ee5\u7ed3\u5408\u8fd9\u4e24\u4e2a\u51fd\u6570\u6765\u83b7\u53d6\u7b2c\u4e00\u4e2a\u5143\u7d20\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">my_list = [1, 2, 3, 4, 5]<\/p>\n<p>first_element = next(iter(my_list))<\/p>\n<p>print(first_element)  # \u8f93\u51fa\uff1a1<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u8ff0\u4ee3\u7801\u4e2d\uff0c<code>iter(my_list)<\/code> \u751f\u6210\u4e86\u5217\u8868 <code>my_list<\/code> \u7684\u8fed\u4ee3\u5668\uff0c\u7136\u540e\u901a\u8fc7 <code>next()<\/code> \u51fd\u6570\u83b7\u53d6\u8fed\u4ee3\u5668\u7684\u7b2c\u4e00\u4e2a\u5143\u7d20\u3002<\/p>\n<\/p>\n<p><h3>\u516d\u3001\u4f7f\u7528 pandas<\/h3>\n<\/p>\n<p><p>\u5982\u679c\u6570\u636e\u4fdd\u5b58\u5728 pandas DataFrame \u6216 Series \u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528 pandas \u7684\u7d22\u5f15\u673a\u5236\u6765\u83b7\u53d6\u7b2c\u4e00\u4e2a\u5143\u7d20\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import pandas as pd<\/p>\n<p>data = pd.Series([1, 2, 3, 4, 5])<\/p>\n<p>first_element = data.iloc[0]<\/p>\n<p>print(first_element)  # \u8f93\u51fa\uff1a1<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u8ff0\u4ee3\u7801\u4e2d\uff0c<code>data.iloc[0]<\/code> \u8868\u793a\u83b7\u53d6 <code>data<\/code> \u4e2d\u7684\u7b2c\u4e00\u4e2a\u5143\u7d20\uff0c<code>iloc<\/code> \u662f pandas \u4e2d\u7684\u7d22\u5f15\u5668\uff0c\u7528\u4e8e\u901a\u8fc7\u4f4d\u7f6e\u7d22\u5f15\u83b7\u53d6\u6570\u636e\u3002<\/p>\n<\/p>\n<p><h3>\u4e03\u3001\u4f7f\u7528 numpy<\/h3>\n<\/p>\n<p><p>\u5982\u679c\u6570\u636e\u4fdd\u5b58\u5728 numpy \u6570\u7ec4\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528 numpy \u7684\u7d22\u5f15\u673a\u5236\u6765\u83b7\u53d6\u7b2c\u4e00\u4e2a\u5143\u7d20\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import numpy as np<\/p>\n<p>data = np.array([1, 2, 3, 4, 5])<\/p>\n<p>first_element = data[0]<\/p>\n<p>print(first_element)  # \u8f93\u51fa\uff1a1<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u8ff0\u4ee3\u7801\u4e2d\uff0c<code>data[0]<\/code> \u8868\u793a\u83b7\u53d6 <code>data<\/code> \u4e2d\u7684\u7b2c\u4e00\u4e2a\u5143\u7d20\u3002<\/p>\n<\/p>\n<p><h3>\u516b\u3001\u4f7f\u7528 collections.deque<\/h3>\n<\/p>\n<p><p><code>collections.deque<\/code> \u662f\u4e00\u4e2a\u53cc\u5411\u961f\u5217\uff0c\u53ef\u4ee5\u4ece\u5de6\u4fa7\u6216\u53f3\u4fa7\u6dfb\u52a0\u6216\u5220\u9664\u5143\u7d20\u3002\u53ef\u4ee5\u901a\u8fc7\u7d22\u5f15\u6765\u83b7\u53d6\u53cc\u5411\u961f\u5217\u4e2d\u7684\u7b2c\u4e00\u4e2a\u5143\u7d20\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from collections import deque<\/p>\n<p>data = deque([1, 2, 3, 4, 5])<\/p>\n<p>first_element = data[0]<\/p>\n<p>print(first_element)  # \u8f93\u51fa\uff1a1<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u8ff0\u4ee3\u7801\u4e2d\uff0c\u901a\u8fc7 <code>data[0]<\/code> \u53ef\u4ee5\u83b7\u53d6\u53cc\u5411\u961f\u5217 <code>data<\/code> \u4e2d\u7684\u7b2c\u4e00\u4e2a\u5143\u7d20\u3002<\/p>\n<\/p>\n<p><h3>\u4e5d\u3001\u4f7f\u7528 heapq<\/h3>\n<\/p>\n<p><p><code>heapq<\/code> \u662f Python \u4e2d\u7684\u4e00\u4e2a\u6a21\u5757\uff0c\u7528\u4e8e\u5b9e\u73b0\u5806\u961f\u5217\u7b97\u6cd5\u3002\u53ef\u4ee5\u5c06\u5217\u8868\u8f6c\u6362\u4e3a\u5806\uff0c\u5e76\u83b7\u53d6\u5806\u4e2d\u7684\u7b2c\u4e00\u4e2a\u5143\u7d20\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import heapq<\/p>\n<p>data = [1, 2, 3, 4, 5]<\/p>\n<p>heapq.heapify(data)<\/p>\n<p>first_element = data[0]<\/p>\n<p>print(first_element)  # \u8f93\u51fa\uff1a1<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u8ff0\u4ee3\u7801\u4e2d\uff0c\u901a\u8fc7 <code>heapq.heapify(data)<\/code> \u5c06\u5217\u8868 <code>data<\/code> \u8f6c\u6362\u4e3a\u5806\uff0c\u7136\u540e\u901a\u8fc7 <code>data[0]<\/code> \u83b7\u53d6\u5806\u4e2d\u7684\u7b2c\u4e00\u4e2a\u5143\u7d20\u3002<\/p>\n<\/p>\n<p><h3>\u5341\u3001\u4f7f\u7528 itertools<\/h3>\n<\/p>\n<p><p><code>itertools<\/code> \u662f Python \u4e2d\u7684\u4e00\u4e2a\u6a21\u5757\uff0c\u63d0\u4f9b\u4e86\u7528\u4e8e\u64cd\u4f5c\u8fed\u4ee3\u5668\u7684\u51fd\u6570\u3002\u53ef\u4ee5\u4f7f\u7528 <code>itertools.islice<\/code> \u6765\u83b7\u53d6\u8fed\u4ee3\u5668\u4e2d\u7684\u7b2c\u4e00\u4e2a\u5143\u7d20\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import itertools<\/p>\n<p>data = [1, 2, 3, 4, 5]<\/p>\n<p>first_element = next(itertools.islice(data, 1))<\/p>\n<p>print(first_element)  # \u8f93\u51fa\uff1a1<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u8ff0\u4ee3\u7801\u4e2d\uff0c\u901a\u8fc7 <code>itertools.islice(data, 1)<\/code> \u83b7\u53d6\u8fed\u4ee3\u5668 <code>data<\/code> \u7684\u7b2c\u4e00\u4e2a\u5143\u7d20\u3002<\/p>\n<\/p>\n<p><h3>\u5341\u4e00\u3001\u4f7f\u7528 map<\/h3>\n<\/p>\n<p><p><code>map<\/code> \u662f Python \u4e2d\u7684\u4e00\u4e2a\u51fd\u6570\uff0c\u7528\u4e8e\u5c06\u51fd\u6570\u5e94\u7528\u4e8e\u53ef\u8fed\u4ee3\u5bf9\u8c61\u7684\u6bcf\u4e2a\u5143\u7d20\u3002\u53ef\u4ee5\u7ed3\u5408 <code>next<\/code> \u548c <code>iter<\/code> \u51fd\u6570\u6765\u83b7\u53d6\u7b2c\u4e00\u4e2a\u5143\u7d20\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">data = [1, 2, 3, 4, 5]<\/p>\n<p>first_element = next(iter(map(lambda x: x, data)))<\/p>\n<p>print(first_element)  # \u8f93\u51fa\uff1a1<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u8ff0\u4ee3\u7801\u4e2d\uff0c\u901a\u8fc7 <code>map(lambda x: x, data)<\/code> \u5c06 <code>data<\/code> \u4e2d\u7684\u6bcf\u4e2a\u5143\u7d20\u6620\u5c04\u4e3a\u81ea\u8eab\uff0c\u7136\u540e\u901a\u8fc7 <code>next(iter(...))<\/code> \u83b7\u53d6\u7b2c\u4e00\u4e2a\u5143\u7d20\u3002<\/p>\n<\/p>\n<p><h3>\u5341\u4e8c\u3001\u4f7f\u7528 filter<\/h3>\n<\/p>\n<p><p><code>filter<\/code> \u662f Python \u4e2d\u7684\u4e00\u4e2a\u51fd\u6570\uff0c\u7528\u4e8e\u8fc7\u6ee4\u53ef\u8fed\u4ee3\u5bf9\u8c61\u4e2d\u7684\u5143\u7d20\u3002\u53ef\u4ee5\u7ed3\u5408 <code>next<\/code> \u548c <code>iter<\/code> \u51fd\u6570\u6765\u83b7\u53d6\u7b2c\u4e00\u4e2a\u5143\u7d20\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">data = [1, 2, 3, 4, 5]<\/p>\n<p>first_element = next(iter(filter(lambda x: True, data)))<\/p>\n<p>print(first_element)  # \u8f93\u51fa\uff1a1<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u8ff0\u4ee3\u7801\u4e2d\uff0c\u901a\u8fc7 <code>filter(lambda x: True, data)<\/code> \u8fc7\u6ee4 <code>data<\/code> \u4e2d\u7684\u6240\u6709\u5143\u7d20\uff0c\u7136\u540e\u901a\u8fc7 <code>next(iter(...))<\/code> \u83b7\u53d6\u7b2c\u4e00\u4e2a\u5143\u7d20\u3002<\/p>\n<\/p>\n<p><h3>\u5341\u4e09\u3001\u4f7f\u7528 enumerate<\/h3>\n<\/p>\n<p><p><code>enumerate<\/code> \u662f Python \u4e2d\u7684\u4e00\u4e2a\u51fd\u6570\uff0c\u7528\u4e8e\u5c06\u53ef\u8fed\u4ee3\u5bf9\u8c61\u7ec4\u5408\u4e3a\u4e00\u4e2a\u7d22\u5f15\u5e8f\u5217\u3002\u53ef\u4ee5\u7ed3\u5408 <code>next<\/code> \u548c <code>iter<\/code> \u51fd\u6570\u6765\u83b7\u53d6\u7b2c\u4e00\u4e2a\u5143\u7d20\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">data = [1, 2, 3, 4, 5]<\/p>\n<p>first_element = next(iter(enumerate(data)))[1]<\/p>\n<p>print(first_element)  # \u8f93\u51fa\uff1a1<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u8ff0\u4ee3\u7801\u4e2d\uff0c\u901a\u8fc7 <code>enumerate(data)<\/code> \u5c06 <code>data<\/code> \u7ec4\u5408\u4e3a\u4e00\u4e2a\u7d22\u5f15\u5e8f\u5217\uff0c\u7136\u540e\u901a\u8fc7 <code>next(iter(...))[1]<\/code> \u83b7\u53d6\u7b2c\u4e00\u4e2a\u5143\u7d20\u3002<\/p>\n<\/p>\n<p><h3>\u5341\u56db\u3001\u4f7f\u7528 set<\/h3>\n<\/p>\n<p><p><code>set<\/code> \u662f Python \u4e2d\u7684\u4e00\u79cd\u96c6\u5408\u7c7b\u578b\uff0c\u53ef\u4ee5\u5b58\u50a8\u65e0\u5e8f\u7684\u552f\u4e00\u5143\u7d20\u3002\u53ef\u4ee5\u5c06\u5217\u8868\u8f6c\u6362\u4e3a\u96c6\u5408\uff0c\u5e76\u83b7\u53d6\u96c6\u5408\u4e2d\u7684\u7b2c\u4e00\u4e2a\u5143\u7d20\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">data = [1, 2, 3, 4, 5]<\/p>\n<p>first_element = next(iter(set(data)))<\/p>\n<p>print(first_element)  # \u8f93\u51fa\uff1a1<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u8ff0\u4ee3\u7801\u4e2d\uff0c\u901a\u8fc7 <code>set(data)<\/code> \u5c06\u5217\u8868 <code>data<\/code> \u8f6c\u6362\u4e3a\u96c6\u5408\uff0c\u7136\u540e\u901a\u8fc7 <code>next(iter(...))<\/code> \u83b7\u53d6\u96c6\u5408\u4e2d\u7684\u7b2c\u4e00\u4e2a\u5143\u7d20\u3002<\/p>\n<\/p>\n<p><h3>\u5341\u4e94\u3001\u4f7f\u7528 dict<\/h3>\n<\/p>\n<p><p><code>dict<\/code> \u662f Python \u4e2d\u7684\u4e00\u79cd\u5b57\u5178\u7c7b\u578b\uff0c\u53ef\u4ee5\u5b58\u50a8\u952e\u503c\u5bf9\u3002\u53ef\u4ee5\u5c06\u5217\u8868\u8f6c\u6362\u4e3a\u5b57\u5178\uff0c\u5e76\u83b7\u53d6\u5b57\u5178\u4e2d\u7684\u7b2c\u4e00\u4e2a\u5143\u7d20\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">data = [1, 2, 3, 4, 5]<\/p>\n<p>data_dict = {i: i for i in data}<\/p>\n<p>first_element = next(iter(data_dict))<\/p>\n<p>print(first_element)  # \u8f93\u51fa\uff1a1<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u8ff0\u4ee3\u7801\u4e2d\uff0c\u901a\u8fc7 <code>{i: i for i in data}<\/code> \u5c06\u5217\u8868 <code>data<\/code> \u8f6c\u6362\u4e3a\u5b57\u5178\uff0c\u7136\u540e\u901a\u8fc7 <code>next(iter(...))<\/code> \u83b7\u53d6\u5b57\u5178\u4e2d\u7684\u7b2c\u4e00\u4e2a\u5143\u7d20\u3002<\/p>\n<\/p>\n<p><p>\u901a\u8fc7\u4e0a\u8ff0\u591a\u79cd\u65b9\u6cd5\uff0c\u53ef\u4ee5\u5728 Python \u4e2d\u8f7b\u677e\u83b7\u53d6\u5e8f\u5217\u4e2d\u7684\u7b2c\u4e00\u4e2a\u5143\u7d20\u3002\u6839\u636e\u4e0d\u540c\u7684\u9700\u6c42\u548c\u573a\u666f\uff0c\u53ef\u4ee5\u9009\u62e9\u6700\u9002\u5408\u7684\u65b9\u6cd5\u6765\u5b9e\u73b0\u8fd9\u4e00\u64cd\u4f5c\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u83b7\u53d6\u5217\u8868\u7684\u7b2c\u4e00\u4e2a\u5143\u7d20\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u60a8\u53ef\u4ee5\u901a\u8fc7\u7d22\u5f15\u8bbf\u95ee\u5217\u8868\u7684\u7b2c\u4e00\u4e2a\u5143\u7d20\u3002\u5217\u8868\u7684\u7d22\u5f15\u4ece0\u5f00\u59cb\uff0c\u56e0\u6b64\u8981\u83b7\u53d6\u7b2c\u4e00\u4e2a\u5143\u7d20\uff0c\u53ea\u9700\u4f7f\u7528<code>list[0]<\/code>\u3002\u4f8b\u5982\uff0c\u5982\u679c\u60a8\u6709\u4e00\u4e2a\u540d\u4e3a<code>my_list<\/code>\u7684\u5217\u8868\uff0c\u60a8\u53ef\u4ee5\u901a\u8fc7<code>first_element = my_list[0]<\/code>\u6765\u83b7\u53d6\u5b83\u3002<\/p>\n<p><strong>\u5728Python\u4e2d\uff0c\u5982\u4f55\u4f7f\u7528\u51fd\u6570\u8fd4\u56de\u7b2c\u4e00\u4e2a\u503c\uff1f<\/strong><br \/>\u60a8\u53ef\u4ee5\u5b9a\u4e49\u4e00\u4e2a\u51fd\u6570\u5e76\u5728\u5176\u4e2d\u4f7f\u7528<code>return<\/code>\u8bed\u53e5\u8fd4\u56de\u5217\u8868\u6216\u5176\u4ed6\u53ef\u8fed\u4ee3\u5bf9\u8c61\u7684\u7b2c\u4e00\u4e2a\u503c\u3002\u4f8b\u5982\uff1a  <\/p>\n<pre><code class=\"language-python\">def get_first_element(my_list):\n    return my_list[0] if my_list else None\n<\/code><\/pre>\n<p>\u8fd9\u4e2a\u51fd\u6570\u5c06\u8fd4\u56de\u5217\u8868\u7684\u7b2c\u4e00\u4e2a\u5143\u7d20\uff0c\u5982\u679c\u5217\u8868\u4e3a\u7a7a\uff0c\u5219\u8fd4\u56de<code>None<\/code>\u3002<\/p>\n<p><strong>\u5982\u679c\u5217\u8868\u4e3a\u7a7a\uff0cPython\u4f1a\u5982\u4f55\u5904\u7406\uff1f<\/strong><br \/>\u5f53\u60a8\u5c1d\u8bd5\u8bbf\u95ee\u7a7a\u5217\u8868\u7684\u7b2c\u4e00\u4e2a\u5143\u7d20\u65f6\uff0cPython\u4f1a\u5f15\u53d1<code>IndexError<\/code>\u3002\u4e3a\u4e86\u907f\u514d\u8fd9\u79cd\u60c5\u51b5\uff0c\u6700\u597d\u5728\u8bbf\u95ee\u5143\u7d20\u4e4b\u524d\u68c0\u67e5\u5217\u8868\u662f\u5426\u4e3a\u7a7a\u3002\u60a8\u53ef\u4ee5\u901a\u8fc7\u6761\u4ef6\u8bed\u53e5\u6765\u5b9e\u73b0\u8fd9\u4e00\u70b9\uff0c\u786e\u4fdd\u4ee3\u7801\u66f4\u52a0\u5065\u58ee\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u4f7f\u7528 Python \u8fd4\u56de\u7b2c\u4e00\u4e2a\u5143\u7d20\u7684\u65b9\u6cd5\u4e3b\u8981\u6709\uff1a\u4f7f\u7528\u7d22\u5f15\u8bbf\u95ee\u3001\u4f7f\u7528\u89e3\u5305\u3001\u4f7f\u7528\u8fed\u4ee3\u5668\u7b49\u65b9\u6cd5\u3002\u4e0b\u9762\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u5176\u4e2d\u4e00 [&hellip;]","protected":false},"author":3,"featured_media":1096602,"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\/1096594"}],"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=1096594"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1096594\/revisions"}],"predecessor-version":[{"id":1096604,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1096594\/revisions\/1096604"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1096602"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1096594"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1096594"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1096594"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}