{"id":1061270,"date":"2024-12-31T15:41:18","date_gmt":"2024-12-31T07:41:18","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1061270.html"},"modified":"2024-12-31T15:41:21","modified_gmt":"2024-12-31T07:41:21","slug":"python%e5%a6%82%e4%bd%95%e8%bf%94%e5%9b%9e%e7%ac%ac%e4%b8%80%e9%83%a8","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1061270.html","title":{"rendered":"python\u5982\u4f55\u8fd4\u56de\u7b2c\u4e00\u90e8"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-docs.pingcode.com\/wp-content\/uploads\/2024\/12\/e9c0a17a-682d-41d4-89fc-f131f7524e05.webp?x-oss-process=image\/auto-orient,1\/format,webp\" alt=\"python\u5982\u4f55\u8fd4\u56de\u7b2c\u4e00\u90e8\" \/><\/p>\n<p><p> <strong>\u5728Python\u4e2d\u8fd4\u56de\u7b2c\u4e00\u6b65\u7684\u65b9\u6cd5\u6709\u591a\u79cd\uff0c\u5177\u4f53\u53d6\u51b3\u4e8e\u4f60\u6240\u6307\u7684\u201c\u7b2c\u4e00\u6b65\u201d\u662f\u4ec0\u4e48\u3002<\/strong>\u4f8b\u5982\uff0c\u5982\u679c\u4f60\u60f3\u8fd4\u56de\u5217\u8868\u4e2d\u7684\u7b2c\u4e00\u4e2a\u5143\u7d20\u3001\u5b57\u5178\u4e2d\u7684\u7b2c\u4e00\u4e2a\u952e\u503c\u5bf9\u3001\u5b57\u7b26\u4e32\u4e2d\u7684\u7b2c\u4e00\u4e2a\u5b57\u7b26\u7b49\uff0c\u5e38\u89c1\u7684\u65b9\u6cd5\u5305\u62ec\uff1a\u4f7f\u7528\u7d22\u5f15\u3001\u4f7f\u7528\u5185\u7f6e\u51fd\u6570\u3001\u5229\u7528\u751f\u6210\u5668\u7b49\u3002\u4e0b\u9762\u6211\u4eec\u5c06\u8be6\u7ec6\u63a2\u8ba8\u8fd9\u4e9b\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<p><p><strong>\u4f7f\u7528\u7d22\u5f15<\/strong><\/p>\n<\/p>\n<p><p>\u5728Python\u4e2d\uff0c\u5217\u8868\u548c\u5b57\u7b26\u4e32\u90fd\u662f\u6709\u5e8f\u7684\u96c6\u5408\uff0c\u53ef\u4ee5\u901a\u8fc7\u7d22\u5f15\u6765\u8bbf\u95ee\u5b83\u4eec\u7684\u5143\u7d20\u3002\u7d22\u5f15\u662f\u4ece0\u5f00\u59cb\u7684\uff0c\u56e0\u6b64\u7b2c\u4e00\u4e2a\u5143\u7d20\u7684\u7d22\u5f15\u662f0\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u5217\u8868<\/p>\n<p>my_list = [1, 2, 3, 4, 5]<\/p>\n<p>first_element = my_list[0]<\/p>\n<p>print(first_element)  # \u8f93\u51fa: 1<\/p>\n<h2><strong>\u5b57\u7b26\u4e32<\/strong><\/h2>\n<p>my_string = &quot;Hello&quot;<\/p>\n<p>first_char = my_string[0]<\/p>\n<p>print(first_char)  # \u8f93\u51fa: H<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>\u4f7f\u7528\u5185\u7f6e\u51fd\u6570<\/strong><\/p>\n<\/p>\n<p><p>\u5bf9\u4e8e\u5b57\u5178\uff0cPython 3.7\u53ca\u4ee5\u540e\u7248\u672c\u7684\u5b57\u5178\u662f\u6709\u5e8f\u7684\uff0c\u53ef\u4ee5\u4f7f\u7528<code>next<\/code>\u548c<code>iter<\/code>\u51fd\u6570\u6765\u83b7\u53d6\u7b2c\u4e00\u4e2a\u952e\u503c\u5bf9\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">my_dict = {&#39;a&#39;: 1, &#39;b&#39;: 2, &#39;c&#39;: 3}<\/p>\n<p>first_key_value = next(iter(my_dict.items()))<\/p>\n<p>print(first_key_value)  # \u8f93\u51fa: (&#39;a&#39;, 1)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>\u5229\u7528\u751f\u6210\u5668<\/strong><\/p>\n<\/p>\n<p><p>\u751f\u6210\u5668\u662f\u4e00\u79cd\u7279\u6b8a\u7684\u8fed\u4ee3\u5668\uff0c\u53ef\u4ee5\u5728\u9700\u8981\u65f6\u751f\u6210\u503c\u3002\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u751f\u6210\u5668\u6765\u521b\u5efa\u4e00\u4e2a\u5e8f\u5217\uff0c\u5e76\u8fd4\u56de\u5e8f\u5217\u4e2d\u7684\u7b2c\u4e00\u4e2a\u503c\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def my_generator():<\/p>\n<p>    yield 1<\/p>\n<p>    yield 2<\/p>\n<p>    yield 3<\/p>\n<p>gen = my_generator()<\/p>\n<p>first_value = next(gen)<\/p>\n<p>print(first_value)  # \u8f93\u51fa: 1<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>\u4e00\u3001\u5217\u8868\u4e2d\u7684\u7b2c\u4e00\u4e2a\u5143\u7d20<\/strong><\/p>\n<\/p>\n<p><p>\u5217\u8868\u662fPython\u4e2d\u5e38\u7528\u7684\u6570\u636e\u7ed3\u6784\u4e4b\u4e00\u3002\u5217\u8868\u4e2d\u7684\u5143\u7d20\u662f\u6709\u5e8f\u7684\uff0c\u53ef\u4ee5\u901a\u8fc7\u7d22\u5f15\u6765\u8bbf\u95ee\u3002\u83b7\u53d6\u5217\u8868\u4e2d\u7684\u7b2c\u4e00\u4e2a\u5143\u7d20\u662f\u6700\u5e38\u89c1\u7684\u64cd\u4f5c\u4e4b\u4e00\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">my_list = [10, 20, 30, 40, 50]<\/p>\n<p>first_element = my_list[0]<\/p>\n<p>print(first_element)  # \u8f93\u51fa: 10<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u521b\u5efa\u4e86\u4e00\u4e2a\u5305\u542b\u4e94\u4e2a\u6574\u6570\u7684\u5217\u8868\uff0c\u7136\u540e\u901a\u8fc7\u7d22\u5f15<code>0<\/code>\u6765\u8bbf\u95ee\u5217\u8868\u4e2d\u7684\u7b2c\u4e00\u4e2a\u5143\u7d20\u3002\u7d22\u5f15<code>0<\/code>\u5bf9\u5e94\u7684\u662f\u5217\u8868\u4e2d\u7684\u7b2c\u4e00\u4e2a\u5143\u7d20\u3002<\/p>\n<\/p>\n<p><p><strong>\u4e8c\u3001\u5b57\u7b26\u4e32\u4e2d\u7684\u7b2c\u4e00\u4e2a\u5b57\u7b26<\/strong><\/p>\n<\/p>\n<p><p>\u5b57\u7b26\u4e32\u662f\u7531\u5b57\u7b26\u7ec4\u6210\u7684\u6709\u5e8f\u5e8f\u5217\u3002\u83b7\u53d6\u5b57\u7b26\u4e32\u4e2d\u7684\u7b2c\u4e00\u4e2a\u5b57\u7b26\u548c\u83b7\u53d6\u5217\u8868\u4e2d\u7684\u7b2c\u4e00\u4e2a\u5143\u7d20\u7c7b\u4f3c\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">my_string = &quot;Python&quot;<\/p>\n<p>first_char = my_string[0]<\/p>\n<p>print(first_char)  # \u8f93\u51fa: P<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u521b\u5efa\u4e86\u4e00\u4e2a\u5b57\u7b26\u4e32<code>&quot;Python&quot;<\/code>\uff0c\u7136\u540e\u901a\u8fc7\u7d22\u5f15<code>0<\/code>\u6765\u8bbf\u95ee\u5b57\u7b26\u4e32\u4e2d\u7684\u7b2c\u4e00\u4e2a\u5b57\u7b26\u3002\u7d22\u5f15<code>0<\/code>\u5bf9\u5e94\u7684\u662f\u5b57\u7b26\u4e32\u4e2d\u7684\u7b2c\u4e00\u4e2a\u5b57\u7b26\u3002<\/p>\n<\/p>\n<p><p><strong>\u4e09\u3001\u5b57\u5178\u4e2d\u7684\u7b2c\u4e00\u4e2a\u952e\u503c\u5bf9<\/strong><\/p>\n<\/p>\n<p><p>\u5728Python 3.7\u53ca\u4ee5\u540e\u7684\u7248\u672c\u4e2d\uff0c\u5b57\u5178\u662f\u6709\u5e8f\u7684\uff0c\u8fd9\u610f\u5473\u7740\u5b57\u5178\u4f1a\u6309\u7167\u63d2\u5165\u987a\u5e8f\u6765\u5b58\u50a8\u952e\u503c\u5bf9\u3002\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528<code>next<\/code>\u548c<code>iter<\/code>\u51fd\u6570\u6765\u83b7\u53d6\u5b57\u5178\u4e2d\u7684\u7b2c\u4e00\u4e2a\u952e\u503c\u5bf9\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">my_dict = {&#39;name&#39;: &#39;Alice&#39;, &#39;age&#39;: 25, &#39;city&#39;: &#39;New York&#39;}<\/p>\n<p>first_key_value = next(iter(my_dict.items()))<\/p>\n<p>print(first_key_value)  # \u8f93\u51fa: (&#39;name&#39;, &#39;Alice&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u521b\u5efa\u4e86\u4e00\u4e2a\u5305\u542b\u4e09\u4e2a\u952e\u503c\u5bf9\u7684\u5b57\u5178\uff0c\u7136\u540e\u4f7f\u7528<code>iter<\/code>\u51fd\u6570\u521b\u5efa\u4e00\u4e2a\u5b57\u5178\u9879\u7684\u8fed\u4ee3\u5668\uff0c\u518d\u4f7f\u7528<code>next<\/code>\u51fd\u6570\u83b7\u53d6\u8fed\u4ee3\u5668\u4e2d\u7684\u7b2c\u4e00\u4e2a\u952e\u503c\u5bf9\u3002<\/p>\n<\/p>\n<p><p><strong>\u56db\u3001\u96c6\u5408\u4e2d\u7684\u7b2c\u4e00\u4e2a\u5143\u7d20<\/strong><\/p>\n<\/p>\n<p><p>\u96c6\u5408\u662f\u4e00\u79cd\u65e0\u5e8f\u7684\u6570\u636e\u7ed3\u6784\uff0c\u56e0\u6b64\u96c6\u5408\u4e2d\u7684\u5143\u7d20\u6ca1\u6709\u56fa\u5b9a\u7684\u987a\u5e8f\u3002\u4e0d\u8fc7\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528<code>next<\/code>\u548c<code>iter<\/code>\u51fd\u6570\u6765\u83b7\u53d6\u96c6\u5408\u4e2d\u7684\u7b2c\u4e00\u4e2a\u5143\u7d20\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">my_set = {1, 2, 3, 4, 5}<\/p>\n<p>first_element = next(iter(my_set))<\/p>\n<p>print(first_element)  # \u8f93\u51fa: 1<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u521b\u5efa\u4e86\u4e00\u4e2a\u5305\u542b\u4e94\u4e2a\u6574\u6570\u7684\u96c6\u5408\uff0c\u7136\u540e\u4f7f\u7528<code>iter<\/code>\u51fd\u6570\u521b\u5efa\u4e00\u4e2a\u96c6\u5408\u9879\u7684\u8fed\u4ee3\u5668\uff0c\u518d\u4f7f\u7528<code>next<\/code>\u51fd\u6570\u83b7\u53d6\u8fed\u4ee3\u5668\u4e2d\u7684\u7b2c\u4e00\u4e2a\u5143\u7d20\u3002<\/p>\n<\/p>\n<p><p><strong>\u4e94\u3001\u961f\u5217\u4e2d\u7684\u7b2c\u4e00\u4e2a\u5143\u7d20<\/strong><\/p>\n<\/p>\n<p><p>\u961f\u5217\u662f\u4e00\u79cd\u5148\u8fdb\u5148\u51fa\u7684\u6570\u636e\u7ed3\u6784\uff0c\u53ef\u4ee5\u4f7f\u7528<code>queue<\/code>\u6a21\u5757\u6765\u5b9e\u73b0\u3002\u83b7\u53d6\u961f\u5217\u4e2d\u7684\u7b2c\u4e00\u4e2a\u5143\u7d20\u662f\u961f\u5217\u64cd\u4f5c\u7684\u5e38\u89c1\u9700\u6c42\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from queue import Queue<\/p>\n<p>my_queue = Queue()<\/p>\n<p>my_queue.put(10)<\/p>\n<p>my_queue.put(20)<\/p>\n<p>my_queue.put(30)<\/p>\n<p>first_element = my_queue.get()<\/p>\n<p>print(first_element)  # \u8f93\u51fa: 10<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528<code>queue<\/code>\u6a21\u5757\u521b\u5efa\u4e86\u4e00\u4e2a\u961f\u5217\uff0c\u5e76\u5411\u961f\u5217\u4e2d\u6dfb\u52a0\u4e86\u4e09\u4e2a\u6574\u6570\uff0c\u7136\u540e\u4f7f\u7528<code>get<\/code>\u65b9\u6cd5\u83b7\u53d6\u961f\u5217\u4e2d\u7684\u7b2c\u4e00\u4e2a\u5143\u7d20\u3002<\/p>\n<\/p>\n<p><p><strong>\u516d\u3001\u4f7f\u7528\u751f\u6210\u5668\u8fd4\u56de\u7b2c\u4e00\u4e2a\u5143\u7d20<\/strong><\/p>\n<\/p>\n<p><p>\u751f\u6210\u5668\u662f\u4e00\u79cd\u7279\u6b8a\u7684\u8fed\u4ee3\u5668\uff0c\u53ef\u4ee5\u5728\u9700\u8981\u65f6\u751f\u6210\u503c\u3002\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u751f\u6210\u5668\u6765\u521b\u5efa\u4e00\u4e2a\u5e8f\u5217\uff0c\u5e76\u8fd4\u56de\u5e8f\u5217\u4e2d\u7684\u7b2c\u4e00\u4e2a\u503c\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def my_generator():<\/p>\n<p>    yield 10<\/p>\n<p>    yield 20<\/p>\n<p>    yield 30<\/p>\n<p>gen = my_generator()<\/p>\n<p>first_value = next(gen)<\/p>\n<p>print(first_value)  # \u8f93\u51fa: 10<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u5b9a\u4e49\u4e86\u4e00\u4e2a\u751f\u6210\u5668\u51fd\u6570<code>my_generator<\/code>\uff0c\u8be5\u51fd\u6570\u4f9d\u6b21\u751f\u6210\u4e09\u4e2a\u6574\u6570\u3002\u6211\u4eec\u521b\u5efa\u4e86\u4e00\u4e2a\u751f\u6210\u5668\u5bf9\u8c61\uff0c\u7136\u540e\u4f7f\u7528<code>next<\/code>\u51fd\u6570\u83b7\u53d6\u751f\u6210\u5668\u4e2d\u7684\u7b2c\u4e00\u4e2a\u503c\u3002<\/p>\n<\/p>\n<p><p><strong>\u4e03\u3001\u4f7f\u7528\u5207\u7247\u8fd4\u56de\u7b2c\u4e00\u4e2a\u5143\u7d20<\/strong><\/p>\n<\/p>\n<p><p>\u5207\u7247\u662f\u4e00\u79cd\u5f3a\u5927\u7684\u64cd\u4f5c\uff0c\u53ef\u4ee5\u7528\u4e8e\u4ece\u5e8f\u5217\u4e2d\u63d0\u53d6\u90e8\u5206\u5143\u7d20\u3002\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u5207\u7247\u6765\u83b7\u53d6\u5217\u8868\u6216\u5b57\u7b26\u4e32\u4e2d\u7684\u7b2c\u4e00\u4e2a\u5143\u7d20\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u5217\u8868<\/p>\n<p>my_list = [10, 20, 30, 40, 50]<\/p>\n<p>first_element = my_list[:1]<\/p>\n<p>print(first_element)  # \u8f93\u51fa: [10]<\/p>\n<h2><strong>\u5b57\u7b26\u4e32<\/strong><\/h2>\n<p>my_string = &quot;Python&quot;<\/p>\n<p>first_char = my_string[:1]<\/p>\n<p>print(first_char)  # \u8f93\u51fa: P<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528\u5207\u7247\u64cd\u4f5c<code>[:1]<\/code>\u6765\u83b7\u53d6\u5217\u8868\u6216\u5b57\u7b26\u4e32\u4e2d\u7684\u7b2c\u4e00\u4e2a\u5143\u7d20\u3002\u5207\u7247\u64cd\u4f5c\u8fd4\u56de\u4e00\u4e2a\u5305\u542b\u6240\u9009\u5143\u7d20\u7684\u65b0\u5217\u8868\u6216\u5b57\u7b26\u4e32\u3002<\/p>\n<\/p>\n<p><p><strong>\u516b\u3001\u4f7f\u7528\u89e3\u5305\u8fd4\u56de\u7b2c\u4e00\u4e2a\u5143\u7d20<\/strong><\/p>\n<\/p>\n<p><p>\u89e3\u5305\u662fPython\u4e2d\u7684\u4e00\u79cd\u8bed\u6cd5\uff0c\u53ef\u4ee5\u5c06\u5e8f\u5217\u4e2d\u7684\u5143\u7d20\u89e3\u5305\u5230\u53d8\u91cf\u4e2d\u3002\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u89e3\u5305\u6765\u83b7\u53d6\u5217\u8868\u6216\u5143\u7ec4\u4e2d\u7684\u7b2c\u4e00\u4e2a\u5143\u7d20\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u5217\u8868<\/p>\n<p>my_list = [10, 20, 30, 40, 50]<\/p>\n<p>first_element, *rest = my_list<\/p>\n<p>print(first_element)  # \u8f93\u51fa: 10<\/p>\n<h2><strong>\u5143\u7ec4<\/strong><\/h2>\n<p>my_tuple = (1, 2, 3, 4, 5)<\/p>\n<p>first_element, *rest = my_tuple<\/p>\n<p>print(first_element)  # \u8f93\u51fa: 1<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528\u89e3\u5305\u8bed\u6cd5<code>first_element, *rest<\/code>\u5c06\u5217\u8868\u6216\u5143\u7ec4\u4e2d\u7684\u7b2c\u4e00\u4e2a\u5143\u7d20\u89e3\u5305\u5230\u53d8\u91cf<code>first_element<\/code>\u4e2d\uff0c\u5176\u4f59\u5143\u7d20\u89e3\u5305\u5230\u53d8\u91cf<code>rest<\/code>\u4e2d\u3002<\/p>\n<\/p>\n<p><p><strong>\u4e5d\u3001\u83b7\u53d6\u6587\u4ef6\u4e2d\u7684\u7b2c\u4e00\u884c<\/strong><\/p>\n<\/p>\n<p><p>\u5728\u5904\u7406\u6587\u4ef6\u65f6\uff0c\u83b7\u53d6\u6587\u4ef6\u4e2d\u7684\u7b2c\u4e00\u884c\u662f\u5e38\u89c1\u7684\u64cd\u4f5c\u3002\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u6587\u4ef6\u5bf9\u8c61\u7684<code>readline<\/code>\u65b9\u6cd5\u6765\u83b7\u53d6\u6587\u4ef6\u4e2d\u7684\u7b2c\u4e00\u884c\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">with open(&#39;example.txt&#39;, &#39;r&#39;) as file:<\/p>\n<p>    first_line = file.readline().strip()<\/p>\n<p>    print(first_line)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528<code>open<\/code>\u51fd\u6570\u6253\u5f00\u4e00\u4e2a\u540d\u4e3a<code>example.txt<\/code>\u7684\u6587\u4ef6\uff0c\u5e76\u4f7f\u7528<code>readline<\/code>\u65b9\u6cd5\u8bfb\u53d6\u6587\u4ef6\u4e2d\u7684\u7b2c\u4e00\u884c\u3002<code>strip<\/code>\u65b9\u6cd5\u7528\u4e8e\u79fb\u9664\u884c\u672b\u7684\u6362\u884c\u7b26\u3002<\/p>\n<\/p>\n<p><p><strong>\u5341\u3001\u83b7\u53d6\u6570\u636e\u5e27\u4e2d\u7684\u7b2c\u4e00\u884c<\/strong><\/p>\n<\/p>\n<p><p>\u5728\u5904\u7406\u6570\u636e\u5206\u6790\u65f6\uff0c\u6570\u636e\u5e27\u662f\u5e38\u7528\u7684\u6570\u636e\u7ed3\u6784\u3002\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528Pandas\u5e93\u6765\u83b7\u53d6\u6570\u636e\u5e27\u4e2d\u7684\u7b2c\u4e00\u884c\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import pandas as pd<\/p>\n<p>data = {&#39;name&#39;: [&#39;Alice&#39;, &#39;Bob&#39;, &#39;Charlie&#39;], &#39;age&#39;: [25, 30, 35]}<\/p>\n<p>df = pd.DataFrame(data)<\/p>\n<p>first_row = df.iloc[0]<\/p>\n<p>print(first_row)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528Pandas\u5e93\u521b\u5efa\u4e86\u4e00\u4e2a\u6570\u636e\u5e27\uff0c\u5e76\u4f7f\u7528<code>iloc<\/code>\u65b9\u6cd5\u83b7\u53d6\u6570\u636e\u5e27\u4e2d\u7684\u7b2c\u4e00\u884c\u3002<code>iloc<\/code>\u65b9\u6cd5\u5141\u8bb8\u6211\u4eec\u57fa\u4e8e\u884c\u7d22\u5f15\u6765\u8bbf\u95ee\u6570\u636e\u5e27\u4e2d\u7684\u884c\u3002<\/p>\n<\/p>\n<p><p>\u901a\u8fc7\u4ee5\u4e0a\u5341\u79cd\u65b9\u6cd5\uff0c\u6211\u4eec\u53ef\u4ee5\u5728\u4e0d\u540c\u7684\u6570\u636e\u7ed3\u6784\u548c\u573a\u666f\u4e2d\u83b7\u53d6\u7b2c\u4e00\u4e2a\u5143\u7d20\u6216\u9879\u3002Python\u63d0\u4f9b\u4e86\u4e30\u5bcc\u7684\u5185\u7f6e\u51fd\u6570\u548c\u6a21\u5757\uff0c\u4f7f\u5f97\u8fd9\u4e9b\u64cd\u4f5c\u53d8\u5f97\u7b80\u5355\u800c\u9ad8\u6548\u3002\u5e0c\u671b\u8fd9\u7bc7\u6587\u7ae0\u80fd\u591f\u5e2e\u52a9\u4f60\u66f4\u597d\u5730\u7406\u89e3\u548c\u4f7f\u7528Python\u4e2d\u7684\u8fd9\u4e9b\u64cd\u4f5c\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5728Python\u4e2d\uff0c\u5982\u4f55\u83b7\u53d6\u5217\u8868\u7684\u7b2c\u4e00\u4e2a\u5143\u7d20\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u83b7\u53d6\u5217\u8868\u7684\u7b2c\u4e00\u4e2a\u5143\u7d20\u975e\u5e38\u7b80\u5355\u3002\u4f60\u53ea\u9700\u4f7f\u7528\u7d22\u5f150\u6765\u8bbf\u95ee\u5217\u8868\u7684\u7b2c\u4e00\u4e2a\u9879\u3002\u4f8b\u5982\uff0c\u5982\u679c\u4f60\u6709\u4e00\u4e2a\u5217\u8868 <code>my_list = [10, 20, 30]<\/code>\uff0c\u53ef\u4ee5\u4f7f\u7528 <code>first_element = my_list[0]<\/code> \u6765\u83b7\u53d6\u7b2c\u4e00\u4e2a\u5143\u7d20\u3002\u8bf7\u6ce8\u610f\uff0cPython\u7684\u7d22\u5f15\u662f\u4ece0\u5f00\u59cb\u7684\uff0c\u56e0\u6b64\u7b2c\u4e00\u4e2a\u5143\u7d20\u7684\u7d22\u5f15\u662f0\u3002<\/p>\n<p><strong>\u5982\u4f55\u5728Python\u4e2d\u8fd4\u56de\u5b57\u7b26\u4e32\u7684\u7b2c\u4e00\u4e2a\u5b57\u7b26\uff1f<\/strong><br \/>\u8981\u83b7\u53d6\u5b57\u7b26\u4e32\u7684\u7b2c\u4e00\u4e2a\u5b57\u7b26\uff0c\u53ef\u4ee5\u4f7f\u7528\u7c7b\u4f3c\u7684\u7d22\u5f15\u65b9\u6cd5\u3002\u6bd4\u5982\uff0c\u5982\u679c\u4f60\u6709\u4e00\u4e2a\u5b57\u7b26\u4e32 <code>my_string = &quot;Hello&quot;<\/code>\uff0c\u53ef\u4ee5\u901a\u8fc7 <code>first_char = my_string[0]<\/code> \u6765\u8fd4\u56de\u7b2c\u4e00\u4e2a\u5b57\u7b26\u3002\u5728\u5904\u7406\u5b57\u7b26\u4e32\u65f6\uff0c\u786e\u4fdd\u5b57\u7b26\u4e32\u4e0d\u4e3a\u7a7a\uff0c\u4ee5\u514d\u5f15\u53d1\u7d22\u5f15\u9519\u8bef\u3002<\/p>\n<p><strong>\u6709\u6ca1\u6709\u529e\u6cd5\u5728Python\u4e2d\u8fd4\u56de\u5b57\u5178\u7684\u7b2c\u4e00\u4e2a\u952e\u6216\u503c\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u901a\u8fc7 <code>next(iter(my_dict))<\/code> \u6765\u83b7\u53d6\u5b57\u5178\u7684\u7b2c\u4e00\u4e2a\u952e\u3002\u5047\u8bbe\u4f60\u6709\u4e00\u4e2a\u5b57\u5178 <code>my_dict = {&#39;a&#39;: 1, &#39;b&#39;: 2}<\/code>\uff0c\u53ef\u4ee5\u901a\u8fc7 <code>first_key = next(iter(my_dict))<\/code> \u6765\u83b7\u53d6\u7b2c\u4e00\u4e2a\u952e\u3002\u5bf9\u4e8e\u503c\uff0c\u53ef\u4ee5\u4f7f\u7528 <code>my_dict[first_key]<\/code> \u6765\u83b7\u53d6\u5bf9\u5e94\u7684\u503c\u3002\u8981\u6ce8\u610f\u7684\u662f\uff0c\u5b57\u5178\u662f\u65e0\u5e8f\u7684\uff0c\u56e0\u6b64\u201c\u7b2c\u4e00\u4e2a\u201d\u952e\u7684\u5b9a\u4e49\u5728\u4e0d\u540c\u7684Python\u7248\u672c\u4e2d\u53ef\u80fd\u4f1a\u6709\u6240\u4e0d\u540c\uff0c\u5c24\u5176\u662f\u5728Python 3.7\u4e4b\u524d\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728Python\u4e2d\u8fd4\u56de\u7b2c\u4e00\u6b65\u7684\u65b9\u6cd5\u6709\u591a\u79cd\uff0c\u5177\u4f53\u53d6\u51b3\u4e8e\u4f60\u6240\u6307\u7684\u201c\u7b2c\u4e00\u6b65\u201d\u662f\u4ec0\u4e48\u3002\u4f8b\u5982\uff0c\u5982\u679c\u4f60\u60f3\u8fd4\u56de\u5217\u8868\u4e2d\u7684\u7b2c\u4e00\u4e2a\u5143 [&hellip;]","protected":false},"author":3,"featured_media":1061284,"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\/1061270"}],"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=1061270"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1061270\/revisions"}],"predecessor-version":[{"id":1061286,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1061270\/revisions\/1061286"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1061284"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1061270"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1061270"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1061270"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}