{"id":1079412,"date":"2025-01-08T12:20:40","date_gmt":"2025-01-08T04:20:40","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1079412.html"},"modified":"2025-01-08T12:21:09","modified_gmt":"2025-01-08T04:21:09","slug":"python%e5%a6%82%e4%bd%95%e5%8f%96%e7%b4%a2%e5%bc%95%e5%ba%8f%e5%88%97%e4%b8%ad%e7%9a%84%e5%80%bc-2","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1079412.html","title":{"rendered":"python\u5982\u4f55\u53d6\u7d22\u5f15\u5e8f\u5217\u4e2d\u7684\u503c"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/24182449\/683efdec-b6e4-4fd1-8147-b1d5bbf53e6a.webp\" alt=\"python\u5982\u4f55\u53d6\u7d22\u5f15\u5e8f\u5217\u4e2d\u7684\u503c\" \/><\/p>\n<p><p> <strong>Python \u5982\u4f55\u53d6\u7d22\u5f15\u5e8f\u5217\u4e2d\u7684\u503c<\/strong><\/p>\n<\/p>\n<p><p>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u901a\u8fc7\u591a\u79cd\u65b9\u5f0f\u4ece\u7d22\u5f15\u5e8f\u5217\u4e2d\u63d0\u53d6\u503c\u3002\u4f8b\u5982\uff0c\u53ef\u4ee5\u4f7f\u7528\u5217\u8868\u3001\u5143\u7ec4\u3001\u5b57\u5178\u3001Pandas DataFrame\u7b49\u6570\u636e\u7ed3\u6784\u6765\u5b58\u50a8\u6570\u636e\uff0c\u7136\u540e\u901a\u8fc7\u7d22\u5f15\u6216\u952e\u503c\u6765\u8bbf\u95ee\u8fd9\u4e9b\u6570\u636e\u3002<strong>\u4f7f\u7528\u7d22\u5f15\u8bbf\u95ee\u5217\u8868\u3001\u4f7f\u7528\u952e\u8bbf\u95ee\u5b57\u5178\u3001\u4f7f\u7528loc\u548ciloc\u8bbf\u95eePandas DataFrame<\/strong>\u662f\u4e00\u4e9b\u5e38\u89c1\u7684\u65b9\u6cd5\u3002\u4ee5\u4e0b\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u8fd9\u4e9b\u65b9\u6cd5\uff0c\u5e76\u4e3e\u4f8b\u8bf4\u660e\u5982\u4f55\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\u4f7f\u7528\u5b83\u4eec\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u4f7f\u7528\u7d22\u5f15\u8bbf\u95ee\u5217\u8868<\/h3>\n<\/p>\n<p><p>\u5217\u8868\u662fPython\u4e2d\u6700\u5e38\u7528\u7684\u6570\u636e\u7ed3\u6784\u4e4b\u4e00\u3002\u53ef\u4ee5\u901a\u8fc7\u7d22\u5f15\u6765\u8bbf\u95ee\u5217\u8868\u4e2d\u7684\u5143\u7d20\uff0c\u7d22\u5f15\u4ece0\u5f00\u59cb\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">my_list = [10, 20, 30, 40, 50]<\/p>\n<h2><strong>\u8bbf\u95ee\u7b2c\u4e00\u4e2a\u5143\u7d20<\/strong><\/h2>\n<p>first_element = my_list[0]<\/p>\n<p>print(first_element)  # \u8f93\u51fa: 10<\/p>\n<h2><strong>\u8bbf\u95ee\u6700\u540e\u4e00\u4e2a\u5143\u7d20<\/strong><\/h2>\n<p>last_element = my_list[-1]<\/p>\n<p>print(last_element)  # \u8f93\u51fa: 50<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u53ef\u4ee5\u4f7f\u7528\u5207\u7247\u8bed\u6cd5\u4ece\u5217\u8868\u4e2d\u83b7\u53d6\u5b50\u5217\u8868\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u83b7\u53d6\u7d22\u5f151\u52303\u7684\u5143\u7d20\uff0c\u4e0d\u5305\u62ec\u7d22\u5f153<\/p>\n<p>sub_list = my_list[1:3]<\/p>\n<p>print(sub_list)  # \u8f93\u51fa: [20, 30]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e8c\u3001\u4f7f\u7528\u952e\u8bbf\u95ee\u5b57\u5178<\/h3>\n<\/p>\n<p><p>\u5b57\u5178\u662f\u4e00\u79cd\u952e\u503c\u5bf9\u7684\u6570\u636e\u7ed3\u6784\uff0c\u53ef\u4ee5\u901a\u8fc7\u952e\u6765\u8bbf\u95ee\u5bf9\u5e94\u7684\u503c\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<h2><strong>\u8bbf\u95ee\u952e\u4e3a&#39;a&#39;\u7684\u503c<\/strong><\/h2>\n<p>value_a = my_dict[&#39;a&#39;]<\/p>\n<p>print(value_a)  # \u8f93\u51fa: 1<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5b57\u5178\u8fd8\u652f\u6301\u4f7f\u7528<code>get<\/code>\u65b9\u6cd5\u6765\u8bbf\u95ee\u503c\uff0c\u8be5\u65b9\u6cd5\u53ef\u4ee5\u5728\u952e\u4e0d\u5b58\u5728\u65f6\u8fd4\u56de\u9ed8\u8ba4\u503c\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u8bbf\u95ee\u952e\u4e3a&#39;d&#39;\u7684\u503c\uff0c\u4e0d\u5b58\u5728\u5219\u8fd4\u56de\u9ed8\u8ba4\u503c0<\/p>\n<p>value_d = my_dict.get(&#39;d&#39;, 0)<\/p>\n<p>print(value_d)  # \u8f93\u51fa: 0<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e09\u3001\u4f7f\u7528loc\u548ciloc\u8bbf\u95eePandas DataFrame<\/h3>\n<\/p>\n<p><p>Pandas\u662f\u4e00\u4e2a\u5f3a\u5927\u7684\u6570\u636e\u5206\u6790\u5e93\uff0cDataFrame\u662fPandas\u4e2d\u7684\u91cd\u8981\u6570\u636e\u7ed3\u6784\uff0c\u53ef\u4ee5\u901a\u8fc7<code>loc<\/code>\u548c<code>iloc<\/code>\u65b9\u6cd5\u6765\u8bbf\u95ee\u6570\u636e\u3002<\/p>\n<\/p>\n<ol>\n<li><strong>\u4f7f\u7528loc\u6309\u6807\u7b7e\u7d22\u5f15\uff1a<\/strong><\/li>\n<\/ol>\n<p><pre><code class=\"language-python\">import pandas as pd<\/p>\n<p>data = {&#39;A&#39;: [1, 2, 3], &#39;B&#39;: [4, 5, 6]}<\/p>\n<p>df = pd.DataFrame(data)<\/p>\n<h2><strong>\u6309\u884c\u6807\u7b7e\u7d22\u5f15<\/strong><\/h2>\n<p>row = df.loc[0]<\/p>\n<p>print(row)<\/p>\n<h2><strong>\u6309\u5217\u6807\u7b7e\u7d22\u5f15<\/strong><\/h2>\n<p>column = df.loc[:, &#39;A&#39;]<\/p>\n<p>print(column)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ol start=\"2\">\n<li><strong>\u4f7f\u7528iloc\u6309\u4f4d\u7f6e\u7d22\u5f15\uff1a<\/strong><\/li>\n<\/ol>\n<p><pre><code class=\"language-python\"># \u6309\u884c\u4f4d\u7f6e\u7d22\u5f15<\/p>\n<p>row = df.iloc[0]<\/p>\n<p>print(row)<\/p>\n<h2><strong>\u6309\u5217\u4f4d\u7f6e\u7d22\u5f15<\/strong><\/h2>\n<p>column = df.iloc[:, 0]<\/p>\n<p>print(column)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u56db\u3001\u4f7f\u7528NumPy\u6570\u7ec4<\/h3>\n<\/p>\n<p><p>NumPy\u662f\u53e6\u4e00\u4e2a\u7528\u4e8e\u79d1\u5b66\u8ba1\u7b97\u7684\u5e93\uff0c\u63d0\u4f9b\u4e86\u9ad8\u6548\u7684\u6570\u7ec4\u64cd\u4f5c\u3002\u53ef\u4ee5\u901a\u8fc7\u7d22\u5f15\u548c\u5207\u7247\u8bbf\u95eeNumPy\u6570\u7ec4\u4e2d\u7684\u5143\u7d20\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import numpy as np<\/p>\n<p>arr = np.array([10, 20, 30, 40, 50])<\/p>\n<h2><strong>\u8bbf\u95ee\u7b2c\u4e00\u4e2a\u5143\u7d20<\/strong><\/h2>\n<p>first_element = arr[0]<\/p>\n<p>print(first_element)  # \u8f93\u51fa: 10<\/p>\n<h2><strong>\u83b7\u53d6\u5b50\u6570\u7ec4<\/strong><\/h2>\n<p>sub_array = arr[1:3]<\/p>\n<p>print(sub_array)  # \u8f93\u51fa: [20 30]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e94\u3001\u4f7f\u7528\u96c6\u5408Set<\/h3>\n<\/p>\n<p><p>\u867d\u7136\u96c6\u5408\u4e2d\u7684\u5143\u7d20\u662f\u65e0\u5e8f\u7684\uff0c\u4f46\u53ef\u4ee5\u4f7f\u7528\u8fed\u4ee3\u6216\u8f6c\u6362\u4e3a\u5217\u8868\u7684\u65b9\u5f0f\u8bbf\u95ee\u96c6\u5408\u4e2d\u7684\u5143\u7d20\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">my_set = {1, 2, 3, 4, 5}<\/p>\n<h2><strong>\u5c06\u96c6\u5408\u8f6c\u6362\u4e3a\u5217\u8868<\/strong><\/h2>\n<p>set_as_list = list(my_set)<\/p>\n<h2><strong>\u8bbf\u95ee\u7b2c\u4e00\u4e2a\u5143\u7d20<\/strong><\/h2>\n<p>first_element = set_as_list[0]<\/p>\n<p>print(first_element)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u516d\u3001\u4f7f\u7528\u5b57\u7b26\u4e32\u7d22\u5f15<\/h3>\n<\/p>\n<p><p>\u5b57\u7b26\u4e32\u53ef\u4ee5\u770b\u4f5c\u5b57\u7b26\u7684\u5e8f\u5217\uff0c\u53ef\u4ee5\u901a\u8fc7\u7d22\u5f15\u548c\u5207\u7247\u6765\u8bbf\u95ee\u5b57\u7b26\u4e32\u4e2d\u7684\u5b57\u7b26\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">my_string = &quot;Hello, World!&quot;<\/p>\n<h2><strong>\u8bbf\u95ee\u7b2c\u4e00\u4e2a\u5b57\u7b26<\/strong><\/h2>\n<p>first_char = my_string[0]<\/p>\n<p>print(first_char)  # \u8f93\u51fa: H<\/p>\n<h2><strong>\u83b7\u53d6\u5b50\u5b57\u7b26\u4e32<\/strong><\/h2>\n<p>sub_string = my_string[1:5]<\/p>\n<p>print(sub_string)  # \u8f93\u51fa: ello<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e03\u3001\u4f7f\u7528\u8fed\u4ee3\u8bbf\u95ee\u5d4c\u5957\u6570\u636e\u7ed3\u6784<\/h3>\n<\/p>\n<p><p>\u5bf9\u4e8e\u5d4c\u5957\u7684\u6570\u636e\u7ed3\u6784\uff0c\u53ef\u4ee5\u4f7f\u7528\u5d4c\u5957\u7684\u7d22\u5f15\u6765\u8bbf\u95ee\u5185\u90e8\u7684\u5143\u7d20\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">nested_list = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]<\/p>\n<h2><strong>\u8bbf\u95ee\u7b2c\u4e8c\u4e2a\u5b50\u5217\u8868\u7684\u7b2c\u4e09\u4e2a\u5143\u7d20<\/strong><\/h2>\n<p>element = nested_list[1][2]<\/p>\n<p>print(element)  # \u8f93\u51fa: 6<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u516b\u3001\u8bbf\u95ee\u591a\u7ef4NumPy\u6570\u7ec4<\/h3>\n<\/p>\n<p><p>NumPy\u652f\u6301\u591a\u7ef4\u6570\u7ec4\uff0c\u53ef\u4ee5\u4f7f\u7528\u591a\u4e2a\u7d22\u5f15\u6765\u8bbf\u95ee\u591a\u7ef4\u6570\u7ec4\u4e2d\u7684\u5143\u7d20\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">multi_arr = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]])<\/p>\n<h2><strong>\u8bbf\u95ee\u7b2c\u4e8c\u884c\u7b2c\u4e09\u5217\u7684\u5143\u7d20<\/strong><\/h2>\n<p>element = multi_arr[1, 2]<\/p>\n<p>print(element)  # \u8f93\u51fa: 6<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e5d\u3001\u4f7f\u7528\u6761\u4ef6\u7d22\u5f15<\/h3>\n<\/p>\n<p><p>\u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\uff0c\u53ef\u80fd\u9700\u8981\u6839\u636e\u6761\u4ef6\u4ece\u6570\u636e\u7ed3\u6784\u4e2d\u63d0\u53d6\u5143\u7d20\u3002\u4f8b\u5982\uff0c\u4ece\u5217\u8868\u4e2d\u63d0\u53d6\u6240\u6709\u6ee1\u8db3\u67d0\u4e2a\u6761\u4ef6\u7684\u5143\u7d20\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">my_list = [10, 20, 30, 40, 50]<\/p>\n<h2><strong>\u63d0\u53d6\u6240\u6709\u5927\u4e8e20\u7684\u5143\u7d20<\/strong><\/h2>\n<p>filtered_list = [x for x in my_list if x &gt; 20]<\/p>\n<p>print(filtered_list)  # \u8f93\u51fa: [30, 40, 50]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u5341\u3001\u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f\u4ece\u5b57\u7b26\u4e32\u4e2d\u63d0\u53d6\u503c<\/h3>\n<\/p>\n<p><p>\u6b63\u5219\u8868\u8fbe\u5f0f\u662f\u4e00\u79cd\u5f3a\u5927\u7684\u6587\u672c\u5904\u7406\u5de5\u5177\uff0c\u53ef\u4ee5\u7528\u4e8e\u4ece\u5b57\u7b26\u4e32\u4e2d\u63d0\u53d6\u7279\u5b9a\u6a21\u5f0f\u7684\u5b50\u4e32\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import re<\/p>\n<p>my_string = &quot;The price is $123.45&quot;<\/p>\n<h2><strong>\u63d0\u53d6\u4ef7\u683c<\/strong><\/h2>\n<p>price = re.findall(r&#39;\\$\\d+\\.\\d{2}&#39;, my_string)<\/p>\n<p>print(price)  # \u8f93\u51fa: [&#39;$123.45&#39;]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u5341\u4e00\u3001\u4f7f\u7528\u5217\u8868\u63a8\u5bfc\u5f0f\u548c\u751f\u6210\u5668\u8868\u8fbe\u5f0f<\/h3>\n<\/p>\n<p><p>\u5217\u8868\u63a8\u5bfc\u5f0f\u548c\u751f\u6210\u5668\u8868\u8fbe\u5f0f\u662f\u7b80\u6d01\u7684\u8bed\u6cd5\uff0c\u7528\u4e8e\u4ece\u5e8f\u5217\u4e2d\u751f\u6210\u65b0\u5217\u8868\u6216\u751f\u6210\u5668\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u5217\u8868\u63a8\u5bfc\u5f0f<\/p>\n<p>squared_list = [x2 for x in range(10)]<\/p>\n<p>print(squared_list)  # \u8f93\u51fa: [0, 1, 4, 9, 16, 25, 36, 49, 64, 81]<\/p>\n<h2><strong>\u751f\u6210\u5668\u8868\u8fbe\u5f0f<\/strong><\/h2>\n<p>squared_gen = (x2 for x in range(10))<\/p>\n<p>print(list(squared_gen))  # \u8f93\u51fa: [0, 1, 4, 9, 16, 25, 36, 49, 64, 81]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u5341\u4e8c\u3001\u4f7f\u7528enumerate\u51fd\u6570<\/h3>\n<\/p>\n<p><p><code>enumerate<\/code>\u51fd\u6570\u4e3a\u53ef\u8fed\u4ee3\u5bf9\u8c61\u63d0\u4f9b\u7d22\u5f15\uff0c\u53ef\u4ee5\u5728\u5faa\u73af\u4e2d\u540c\u65f6\u83b7\u53d6\u5143\u7d20\u53ca\u5176\u7d22\u5f15\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">my_list = [&#39;a&#39;, &#39;b&#39;, &#39;c&#39;]<\/p>\n<p>for index, value in enumerate(my_list):<\/p>\n<p>    print(f&quot;Index: {index}, Value: {value}&quot;)<\/p>\n<h2><strong>\u8f93\u51fa:<\/strong><\/h2>\n<h2><strong>Index: 0, Value: a<\/strong><\/h2>\n<h2><strong>Index: 1, Value: b<\/strong><\/h2>\n<h2><strong>Index: 2, Value: c<\/strong><\/h2>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u5341\u4e09\u3001\u4f7f\u7528itertools\u6a21\u5757<\/h3>\n<\/p>\n<p><p><code>itertools<\/code>\u6a21\u5757\u63d0\u4f9b\u4e86\u591a\u79cd\u5de5\u5177\u6765\u64cd\u4f5c\u8fed\u4ee3\u5668\uff0c\u53ef\u4ee5\u7528\u4e8e\u4ece\u7d22\u5f15\u5e8f\u5217\u4e2d\u63d0\u53d6\u503c\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import itertools<\/p>\n<h2><strong>\u751f\u6210\u65e0\u9650\u5e8f\u5217<\/strong><\/h2>\n<p>counter = itertools.count(start=0, step=2)<\/p>\n<p>for _ in range(5):<\/p>\n<p>    print(next(counter))<\/p>\n<h2><strong>\u8f93\u51fa:<\/strong><\/h2>\n<h2><strong>0<\/strong><\/h2>\n<h2><strong>2<\/strong><\/h2>\n<h2><strong>4<\/strong><\/h2>\n<h2><strong>6<\/strong><\/h2>\n<h2><strong>8<\/strong><\/h2>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u5341\u56db\u3001\u8bbf\u95ee\u81ea\u5b9a\u4e49\u7c7b\u7684\u5c5e\u6027<\/h3>\n<\/p>\n<p><p>\u5728\u81ea\u5b9a\u4e49\u7c7b\u4e2d\uff0c\u53ef\u4ee5\u901a\u8fc7\u5c5e\u6027\u548c\u65b9\u6cd5\u8bbf\u95ee\u7c7b\u7684\u5185\u90e8\u6570\u636e\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">class MyClass:<\/p>\n<p>    def __init__(self, value):<\/p>\n<p>        self.value = value<\/p>\n<p>    def get_value(self):<\/p>\n<p>        return self.value<\/p>\n<p>obj = MyClass(10)<\/p>\n<p>print(obj.value)  # \u901a\u8fc7\u5c5e\u6027\u8bbf\u95ee<\/p>\n<p>print(obj.get_value())  # \u901a\u8fc7\u65b9\u6cd5\u8bbf\u95ee<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u5341\u4e94\u3001\u4f7f\u7528\u96c6\u5408\u7684\u9ad8\u7ea7\u64cd\u4f5c<\/h3>\n<\/p>\n<p><p>\u96c6\u5408\u63d0\u4f9b\u4e86\u4e00\u4e9b\u9ad8\u7ea7\u7684\u64cd\u4f5c\uff0c\u53ef\u4ee5\u7528\u4e8e\u4ece\u96c6\u5408\u4e2d\u63d0\u53d6\u7279\u5b9a\u7684\u503c\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">set_a = {1, 2, 3, 4, 5}<\/p>\n<p>set_b = {4, 5, 6, 7, 8}<\/p>\n<h2><strong>\u4ea4\u96c6<\/strong><\/h2>\n<p>intersection = set_a &amp; set_b<\/p>\n<p>print(intersection)  # \u8f93\u51fa: {4, 5}<\/p>\n<h2><strong>\u5e76\u96c6<\/strong><\/h2>\n<p>union = set_a | set_b<\/p>\n<p>print(union)  # \u8f93\u51fa: {1, 2, 3, 4, 5, 6, 7, 8}<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u7ed3\u8bba<\/h3>\n<\/p>\n<p><p>\u5728Python\u4e2d\uff0c\u6709\u591a\u79cd\u65b9\u6cd5\u53ef\u4ee5\u4ece\u7d22\u5f15\u5e8f\u5217\u4e2d\u63d0\u53d6\u503c\uff0c\u5305\u62ec<strong>\u4f7f\u7528\u7d22\u5f15\u8bbf\u95ee\u5217\u8868\u3001\u4f7f\u7528\u952e\u8bbf\u95ee\u5b57\u5178\u3001\u4f7f\u7528loc\u548ciloc\u8bbf\u95eePandas DataFrame\u3001\u4f7f\u7528NumPy\u6570\u7ec4<\/strong>\u7b49\u3002\u6bcf\u79cd\u65b9\u6cd5\u90fd\u6709\u5176\u9002\u7528\u7684\u573a\u666f\u548c\u4f18\u52bf\uff0c\u9009\u62e9\u5408\u9002\u7684\u65b9\u6cd5\u53ef\u4ee5\u63d0\u9ad8\u4ee3\u7801\u7684\u6548\u7387\u548c\u53ef\u8bfb\u6027\u3002\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u6839\u636e\u6570\u636e\u7ed3\u6784\u7684\u7279\u70b9\u548c\u9700\u6c42\uff0c\u7075\u6d3b\u8fd0\u7528\u8fd9\u4e9b\u65b9\u6cd5\uff0c\u53ef\u4ee5\u6709\u6548\u5730\u5904\u7406\u548c\u63d0\u53d6\u6570\u636e\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u83b7\u53d6\u7279\u5b9a\u7d22\u5f15\u7684\u503c\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528\u5217\u8868\u6216\u6570\u7ec4\u7684\u7d22\u5f15\u6765\u83b7\u53d6\u7279\u5b9a\u4f4d\u7f6e\u7684\u503c\u3002\u5bf9\u4e8e\u5217\u8868\uff0c\u60a8\u53ef\u4ee5\u4f7f\u7528\u65b9\u62ec\u53f7<code>[]<\/code>\u6765\u6307\u5b9a\u7d22\u5f15\uff0c\u4f8b\u5982<code>my_list[2]<\/code>\u5c06\u8fd4\u56de\u5217\u8868\u4e2d\u7b2c\u4e09\u4e2a\u5143\u7d20\uff08\u7d22\u5f15\u4ece0\u5f00\u59cb\uff09\u3002\u5bf9\u4e8eNumPy\u6570\u7ec4\uff0c\u4f7f\u7528\u76f8\u540c\u7684\u65b9\u6cd5\u4e5f\u53ef\u4ee5\u83b7\u53d6\u503c\uff0c\u6b64\u5916\u8fd8\u53ef\u4ee5\u4f7f\u7528\u5207\u7247\u83b7\u53d6\u591a\u4e2a\u503c\u3002<\/p>\n<p><strong>\u5728Python\u4e2d\u5982\u4f55\u5904\u7406\u8d1f\u7d22\u5f15\uff1f<\/strong><br \/>\u8d1f\u7d22\u5f15\u5728Python\u4e2d\u662f\u975e\u5e38\u6709\u7528\u7684\uff0c\u5b83\u5141\u8bb8\u60a8\u4ece\u5217\u8868\u6216\u6570\u7ec4\u7684\u672b\u5c3e\u5f00\u59cb\u8bbf\u95ee\u5143\u7d20\u3002\u4f8b\u5982\uff0c<code>my_list[-1]<\/code>\u5c06\u8fd4\u56de\u5217\u8868\u7684\u6700\u540e\u4e00\u4e2a\u5143\u7d20\uff0c\u800c<code>my_list[-2]<\/code>\u5c06\u8fd4\u56de\u5012\u6570\u7b2c\u4e8c\u4e2a\u5143\u7d20\u3002\u8fd9\u79cd\u65b9\u5f0f\u4f7f\u5f97\u5728\u4e0d\u77e5\u9053\u5217\u8868\u957f\u5ea6\u7684\u60c5\u51b5\u4e0b\u4e5f\u80fd\u65b9\u4fbf\u5730\u8bbf\u95ee\u5143\u7d20\u3002<\/p>\n<p><strong>\u5982\u679c\u7d22\u5f15\u8d85\u51fa\u4e86\u8303\u56f4\uff0cPython\u4f1a\u53d1\u751f\u4ec0\u4e48\uff1f<\/strong><br \/>\u5f53\u60a8\u5c1d\u8bd5\u8bbf\u95ee\u8d85\u51fa\u5217\u8868\u6216\u6570\u7ec4\u8303\u56f4\u7684\u7d22\u5f15\u65f6\uff0cPython\u4f1a\u5f15\u53d1<code>IndexError<\/code>\u5f02\u5e38\u3002\u8fd9\u901a\u5e38\u610f\u5473\u7740\u60a8\u9700\u8981\u68c0\u67e5\u60a8\u6b63\u5728\u4f7f\u7528\u7684\u7d22\u5f15\u662f\u5426\u5728\u6709\u6548\u8303\u56f4\u5185\u3002\u53ef\u4ee5\u4f7f\u7528<code>len()<\/code>\u51fd\u6570\u6765\u83b7\u53d6\u5217\u8868\u6216\u6570\u7ec4\u7684\u957f\u5ea6\uff0c\u4ece\u800c\u907f\u514d\u8fd9\u79cd\u9519\u8bef\u3002\u4f8b\u5982\uff0c\u786e\u4fdd\u6240\u7528\u7d22\u5f15\u5c0f\u4e8e<code>len(my_list)<\/code>\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"Python \u5982\u4f55\u53d6\u7d22\u5f15\u5e8f\u5217\u4e2d\u7684\u503c \u5728Python\u4e2d\uff0c\u53ef\u4ee5\u901a\u8fc7\u591a\u79cd\u65b9\u5f0f\u4ece\u7d22\u5f15\u5e8f\u5217\u4e2d\u63d0\u53d6\u503c\u3002\u4f8b\u5982\uff0c\u53ef\u4ee5\u4f7f\u7528\u5217\u8868\u3001 [&hellip;]","protected":false},"author":3,"featured_media":1079473,"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\/1079412"}],"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=1079412"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1079412\/revisions"}],"predecessor-version":[{"id":1079478,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1079412\/revisions\/1079478"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1079473"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1079412"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1079412"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1079412"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}