{"id":1069633,"date":"2025-01-08T10:50:46","date_gmt":"2025-01-08T02:50:46","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1069633.html"},"modified":"2025-01-08T10:50:48","modified_gmt":"2025-01-08T02:50:48","slug":"python%e4%b8%ad%e5%9c%a8%e7%b1%bb%e9%87%8c%e5%a6%82%e4%bd%95%e7%94%a8%e5%88%97%e8%a1%a8-2","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1069633.html","title":{"rendered":"Python\u4e2d\u5728\u7c7b\u91cc\u5982\u4f55\u7528\u5217\u8868"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25100558\/a00c0b1a-30ef-476e-b77c-f7aa6a153601.webp\" alt=\"Python\u4e2d\u5728\u7c7b\u91cc\u5982\u4f55\u7528\u5217\u8868\" \/><\/p>\n<p><p> <strong>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u5728\u7c7b\u4e2d\u4f7f\u7528\u5217\u8868\uff0c\u901a\u8fc7\u521d\u59cb\u5316\u5217\u8868\u3001\u6dfb\u52a0\u5143\u7d20\u3001\u8bbf\u95ee\u5143\u7d20\u548c\u4fee\u6539\u5143\u7d20\u7b49\u64cd\u4f5c\u6765\u5b9e\u73b0\u590d\u6742\u7684\u6570\u636e\u7ed3\u6784\u7684\u7ba1\u7406\u3001\u5728\u7c7b\u4e2d\u5b9a\u4e49\u4e00\u4e2a\u5217\u8868\u5e76\u5728\u7c7b\u7684\u65b9\u6cd5\u4e2d\u5bf9\u5176\u8fdb\u884c\u64cd\u4f5c\u3001\u53ef\u4ee5\u5b9e\u73b0\u6570\u636e\u7684\u5c01\u88c5\u4e0e\u903b\u8f91\u7684\u5206\u79bb<\/strong>\u3002\u4e0b\u9762\u6211\u4eec\u5c06\u8be6\u7ec6\u63a2\u8ba8\u5728Python\u7c7b\u4e2d\u5982\u4f55\u4f7f\u7528\u5217\u8868\uff0c\u5e76\u901a\u8fc7\u4e00\u4e9b\u793a\u4f8b\u4ee3\u7801\u8bf4\u660e\u5982\u4f55\u5728\u7c7b\u4e2d\u64cd\u4f5c\u5217\u8868\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u521d\u59cb\u5316\u5217\u8868<\/h3>\n<\/p>\n<p><p>\u5728Python\u7c7b\u4e2d\uff0c\u5217\u8868\u901a\u5e38\u662f\u5728\u7c7b\u7684\u6784\u9020\u51fd\u6570\u4e2d\u8fdb\u884c\u521d\u59cb\u5316\u7684\u3002\u6784\u9020\u51fd\u6570\u662f\u4e00\u4e2a\u7279\u6b8a\u7684\u65b9\u6cd5\uff0c\u540d\u4e3a<code>__init__<\/code>\uff0c\u5b83\u5728\u521b\u5efa\u7c7b\u7684\u5b9e\u4f8b\u65f6\u88ab\u8c03\u7528\u3002\u901a\u8fc7\u5728\u6784\u9020\u51fd\u6570\u4e2d\u521d\u59cb\u5316\u5217\u8868\uff0c\u53ef\u4ee5\u786e\u4fdd\u6bcf\u4e2a\u5b9e\u4f8b\u90fd\u6709\u4e00\u4e2a\u72ec\u7acb\u7684\u5217\u8868\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">class MyClass:<\/p>\n<p>    def __init__(self):<\/p>\n<p>        self.my_list = []<\/p>\n<h2><strong>\u793a\u4f8b<\/strong><\/h2>\n<p>obj = MyClass()<\/p>\n<p>print(obj.my_list)  # \u8f93\u51fa: []<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u9762\u7684\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u5b9a\u4e49\u4e86\u4e00\u4e2a\u7c7b<code>MyClass<\/code>\uff0c\u5728\u5176\u6784\u9020\u51fd\u6570\u4e2d\u521d\u59cb\u5316\u4e86\u4e00\u4e2a\u7a7a\u5217\u8868<code>my_list<\/code>\u3002\u5f53\u521b\u5efa<code>MyClass<\/code>\u7684\u5b9e\u4f8b\u65f6\uff0c\u6bcf\u4e2a\u5b9e\u4f8b\u90fd\u4f1a\u6709\u4e00\u4e2a\u72ec\u7acb\u7684\u7a7a\u5217\u8868\u3002<\/p>\n<\/p>\n<p><h3>\u4e8c\u3001\u5411\u5217\u8868\u4e2d\u6dfb\u52a0\u5143\u7d20<\/h3>\n<\/p>\n<p><p>\u5728\u7c7b\u4e2d\uff0c\u53ef\u4ee5\u901a\u8fc7\u5b9a\u4e49\u65b9\u6cd5\u6765\u5411\u5217\u8868\u4e2d\u6dfb\u52a0\u5143\u7d20\u3002\u5e38\u89c1\u7684\u65b9\u6cd5\u5305\u62ec<code>append<\/code>\u3001<code>extend<\/code>\u548c<code>insert<\/code>\u7b49\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">class MyClass:<\/p>\n<p>    def __init__(self):<\/p>\n<p>        self.my_list = []<\/p>\n<p>    def add_element(self, element):<\/p>\n<p>        self.my_list.append(element)<\/p>\n<h2><strong>\u793a\u4f8b<\/strong><\/h2>\n<p>obj = MyClass()<\/p>\n<p>obj.add_element(10)<\/p>\n<p>obj.add_element(20)<\/p>\n<p>print(obj.my_list)  # \u8f93\u51fa: [10, 20]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u9762\u7684\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u5b9a\u4e49\u4e86\u4e00\u4e2a\u65b9\u6cd5<code>add_element<\/code>\uff0c\u8be5\u65b9\u6cd5\u63a5\u53d7\u4e00\u4e2a\u53c2\u6570\u5e76\u5c06\u5176\u6dfb\u52a0\u5230<code>my_list<\/code>\u4e2d\u3002\u901a\u8fc7\u8c03\u7528<code>add_element<\/code>\u65b9\u6cd5\uff0c\u53ef\u4ee5\u5411\u5217\u8868\u4e2d\u6dfb\u52a0\u5143\u7d20\u3002<\/p>\n<\/p>\n<p><h3>\u4e09\u3001\u8bbf\u95ee\u5217\u8868\u4e2d\u7684\u5143\u7d20<\/h3>\n<\/p>\n<p><p>\u53ef\u4ee5\u901a\u8fc7\u7d22\u5f15\u6765\u8bbf\u95ee\u5217\u8868\u4e2d\u7684\u5143\u7d20\u3002\u6211\u4eec\u53ef\u4ee5\u5b9a\u4e49\u65b9\u6cd5\u6765\u8fd4\u56de\u5217\u8868\u4e2d\u7684\u67d0\u4e2a\u5143\u7d20\u6216\u6574\u4e2a\u5217\u8868\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">class MyClass:<\/p>\n<p>    def __init__(self):<\/p>\n<p>        self.my_list = []<\/p>\n<p>    def add_element(self, element):<\/p>\n<p>        self.my_list.append(element)<\/p>\n<p>    def get_element(self, index):<\/p>\n<p>        if index &lt; len(self.my_list):<\/p>\n<p>            return self.my_list[index]<\/p>\n<p>        else:<\/p>\n<p>            return None<\/p>\n<p>    def get_all_elements(self):<\/p>\n<p>        return self.my_list<\/p>\n<h2><strong>\u793a\u4f8b<\/strong><\/h2>\n<p>obj = MyClass()<\/p>\n<p>obj.add_element(10)<\/p>\n<p>obj.add_element(20)<\/p>\n<p>print(obj.get_element(0))  # \u8f93\u51fa: 10<\/p>\n<p>print(obj.get_all_elements())  # \u8f93\u51fa: [10, 20]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u9762\u7684\u793a\u4f8b\u4e2d\uff0c<code>get_element<\/code>\u65b9\u6cd5\u63a5\u53d7\u4e00\u4e2a\u7d22\u5f15\u53c2\u6570\u5e76\u8fd4\u56de\u5217\u8868\u4e2d\u7684\u76f8\u5e94\u5143\u7d20\uff0c\u5982\u679c\u7d22\u5f15\u8d85\u51fa\u8303\u56f4\uff0c\u5219\u8fd4\u56de<code>None<\/code>\u3002<code>get_all_elements<\/code>\u65b9\u6cd5\u8fd4\u56de\u6574\u4e2a\u5217\u8868\u3002<\/p>\n<\/p>\n<p><h3>\u56db\u3001\u4fee\u6539\u5217\u8868\u4e2d\u7684\u5143\u7d20<\/h3>\n<\/p>\n<p><p>\u53ef\u4ee5\u901a\u8fc7\u7d22\u5f15\u6765\u4fee\u6539\u5217\u8868\u4e2d\u7684\u5143\u7d20\u3002\u6211\u4eec\u53ef\u4ee5\u5b9a\u4e49\u65b9\u6cd5\u6765\u66f4\u65b0\u5217\u8868\u4e2d\u7684\u67d0\u4e2a\u5143\u7d20\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">class MyClass:<\/p>\n<p>    def __init__(self):<\/p>\n<p>        self.my_list = []<\/p>\n<p>    def add_element(self, element):<\/p>\n<p>        self.my_list.append(element)<\/p>\n<p>    def update_element(self, index, new_value):<\/p>\n<p>        if index &lt; len(self.my_list):<\/p>\n<p>            self.my_list[index] = new_value<\/p>\n<p>        else:<\/p>\n<p>            print(&quot;\u7d22\u5f15\u8d85\u51fa\u8303\u56f4&quot;)<\/p>\n<h2><strong>\u793a\u4f8b<\/strong><\/h2>\n<p>obj = MyClass()<\/p>\n<p>obj.add_element(10)<\/p>\n<p>obj.add_element(20)<\/p>\n<p>obj.update_element(1, 30)<\/p>\n<p>print(obj.my_list)  # \u8f93\u51fa: [10, 30]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u9762\u7684\u793a\u4f8b\u4e2d\uff0c<code>update_element<\/code>\u65b9\u6cd5\u63a5\u53d7\u7d22\u5f15\u548c\u65b0\u503c\u4f5c\u4e3a\u53c2\u6570\uff0c\u5e76\u66f4\u65b0\u5217\u8868\u4e2d\u76f8\u5e94\u7d22\u5f15\u5904\u7684\u5143\u7d20\u3002\u5982\u679c\u7d22\u5f15\u8d85\u51fa\u8303\u56f4\uff0c\u5219\u6253\u5370\u9519\u8bef\u6d88\u606f\u3002<\/p>\n<\/p>\n<p><h3>\u4e94\u3001\u5220\u9664\u5217\u8868\u4e2d\u7684\u5143\u7d20<\/h3>\n<\/p>\n<p><p>\u53ef\u4ee5\u901a\u8fc7\u7d22\u5f15\u6216\u503c\u6765\u5220\u9664\u5217\u8868\u4e2d\u7684\u5143\u7d20\u3002\u6211\u4eec\u53ef\u4ee5\u5b9a\u4e49\u65b9\u6cd5\u6765\u5220\u9664\u5217\u8868\u4e2d\u7684\u67d0\u4e2a\u5143\u7d20\u6216\u6e05\u7a7a\u5217\u8868\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">class MyClass:<\/p>\n<p>    def __init__(self):<\/p>\n<p>        self.my_list = []<\/p>\n<p>    def add_element(self, element):<\/p>\n<p>        self.my_list.append(element)<\/p>\n<p>    def remove_element_by_index(self, index):<\/p>\n<p>        if index &lt; len(self.my_list):<\/p>\n<p>            self.my_list.pop(index)<\/p>\n<p>        else:<\/p>\n<p>            print(&quot;\u7d22\u5f15\u8d85\u51fa\u8303\u56f4&quot;)<\/p>\n<p>    def remove_element_by_value(self, value):<\/p>\n<p>        if value in self.my_list:<\/p>\n<p>            self.my_list.remove(value)<\/p>\n<p>        else:<\/p>\n<p>            print(&quot;\u503c\u4e0d\u5b58\u5728\u4e8e\u5217\u8868\u4e2d&quot;)<\/p>\n<p>    def clear_elements(self):<\/p>\n<p>        self.my_list.clear()<\/p>\n<h2><strong>\u793a\u4f8b<\/strong><\/h2>\n<p>obj = MyClass()<\/p>\n<p>obj.add_element(10)<\/p>\n<p>obj.add_element(20)<\/p>\n<p>obj.remove_element_by_index(0)<\/p>\n<p>print(obj.my_list)  # \u8f93\u51fa: [20]<\/p>\n<p>obj.remove_element_by_value(20)<\/p>\n<p>print(obj.my_list)  # \u8f93\u51fa: []<\/p>\n<p>obj.add_element(30)<\/p>\n<p>obj.clear_elements()<\/p>\n<p>print(obj.my_list)  # \u8f93\u51fa: []<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u9762\u7684\u793a\u4f8b\u4e2d\uff0c<code>remove_element_by_index<\/code>\u65b9\u6cd5\u901a\u8fc7\u7d22\u5f15\u5220\u9664\u5217\u8868\u4e2d\u7684\u5143\u7d20\uff0c\u5982\u679c\u7d22\u5f15\u8d85\u51fa\u8303\u56f4\uff0c\u5219\u6253\u5370\u9519\u8bef\u6d88\u606f\u3002<code>remove_element_by_value<\/code>\u65b9\u6cd5\u901a\u8fc7\u503c\u5220\u9664\u5217\u8868\u4e2d\u7684\u5143\u7d20\uff0c\u5982\u679c\u503c\u4e0d\u5b58\u5728\u4e8e\u5217\u8868\u4e2d\uff0c\u5219\u6253\u5370\u9519\u8bef\u6d88\u606f\u3002<code>clear_elements<\/code>\u65b9\u6cd5\u6e05\u7a7a\u5217\u8868\u4e2d\u7684\u6240\u6709\u5143\u7d20\u3002<\/p>\n<\/p>\n<p><h3>\u516d\u3001\u5728\u7c7b\u4e2d\u4f7f\u7528\u5217\u8868\u7684\u5b9e\u9645\u5e94\u7528<\/h3>\n<\/p>\n<p><p>\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u7c7b\u4e2d\u7684\u5217\u8868\u53ef\u4ee5\u7528\u4e8e\u5b58\u50a8\u548c\u7ba1\u7406\u590d\u6742\u7684\u6570\u636e\u7ed3\u6784\u3002\u4f8b\u5982\uff0c\u53ef\u4ee5\u4f7f\u7528\u5217\u8868\u6765\u5b58\u50a8\u548c\u7ba1\u7406\u5b66\u751f\u7684\u6210\u7ee9\u3001\u8ba2\u5355\u5217\u8868\u3001\u4efb\u52a1\u5217\u8868\u7b49\u3002\u4e0b\u9762\u6211\u4eec\u901a\u8fc7\u4e00\u4e2a\u5b66\u751f\u6210\u7ee9\u7ba1\u7406\u7cfb\u7edf\u7684\u793a\u4f8b\u6765\u8bf4\u660e\u5982\u4f55\u5728\u7c7b\u4e2d\u4f7f\u7528\u5217\u8868\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">class Student:<\/p>\n<p>    def __init__(self, name):<\/p>\n<p>        self.name = name<\/p>\n<p>        self.grades = []<\/p>\n<p>    def add_grade(self, grade):<\/p>\n<p>        self.grades.append(grade)<\/p>\n<p>    def get_average_grade(self):<\/p>\n<p>        if len(self.grades) == 0:<\/p>\n<p>            return 0<\/p>\n<p>        return sum(self.grades) \/ len(self.grades)<\/p>\n<p>    def get_grades(self):<\/p>\n<p>        return self.grades<\/p>\n<h2><strong>\u793a\u4f8b<\/strong><\/h2>\n<p>student = Student(&quot;John Doe&quot;)<\/p>\n<p>student.add_grade(90)<\/p>\n<p>student.add_grade(85)<\/p>\n<p>student.add_grade(78)<\/p>\n<p>print(f&quot;{student.name}\u7684\u6210\u7ee9: {student.get_grades()}&quot;)  # \u8f93\u51fa: John Doe\u7684\u6210\u7ee9: [90, 85, 78]<\/p>\n<p>print(f&quot;{student.name}\u7684\u5e73\u5747\u6210\u7ee9: {student.get_average_grade()}&quot;)  # \u8f93\u51fa: John Doe\u7684\u5e73\u5747\u6210\u7ee9: 84.33333333333333<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u9762\u7684\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u5b9a\u4e49\u4e86\u4e00\u4e2a<code>Student<\/code>\u7c7b\uff0c\u8be5\u7c7b\u7528\u4e8e\u7ba1\u7406\u5b66\u751f\u7684\u6210\u7ee9\u3002\u901a\u8fc7\u5728\u6784\u9020\u51fd\u6570\u4e2d\u521d\u59cb\u5316<code>grades<\/code>\u5217\u8868\uff0c\u5e76\u5b9a\u4e49\u65b9\u6cd5<code>add_grade<\/code>\u3001<code>get_average_grade<\/code>\u548c<code>get_grades<\/code>\uff0c\u6211\u4eec\u53ef\u4ee5\u65b9\u4fbf\u5730\u6dfb\u52a0\u6210\u7ee9\u3001\u8ba1\u7b97\u5e73\u5747\u6210\u7ee9\u548c\u83b7\u53d6\u6240\u6709\u6210\u7ee9\u3002<\/p>\n<\/p>\n<p><h3>\u4e03\u3001\u5728\u7c7b\u4e2d\u4f7f\u7528\u5d4c\u5957\u5217\u8868<\/h3>\n<\/p>\n<p><p>\u6709\u65f6\uff0c\u5217\u8868\u4e2d\u7684\u5143\u7d20\u672c\u8eab\u4e5f\u662f\u5217\u8868\uff0c\u8fd9\u79cd\u60c5\u51b5\u79f0\u4e3a\u5d4c\u5957\u5217\u8868\u3002\u5728\u7c7b\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528\u5d4c\u5957\u5217\u8868\u6765\u7ba1\u7406\u591a\u7ef4\u6570\u636e\u3002\u4f8b\u5982\uff0c\u53ef\u4ee5\u4f7f\u7528\u5d4c\u5957\u5217\u8868\u6765\u8868\u793a\u77e9\u9635\u6216\u8868\u683c\u6570\u636e\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">class Matrix:<\/p>\n<p>    def __init__(self, rows, cols):<\/p>\n<p>        self.matrix = [[0] * cols for _ in range(rows)]<\/p>\n<p>    def set_value(self, row, col, value):<\/p>\n<p>        if row &lt; len(self.matrix) and col &lt; len(self.matrix[0]):<\/p>\n<p>            self.matrix[row][col] = value<\/p>\n<p>        else:<\/p>\n<p>            print(&quot;\u7d22\u5f15\u8d85\u51fa\u8303\u56f4&quot;)<\/p>\n<p>    def get_value(self, row, col):<\/p>\n<p>        if row &lt; len(self.matrix) and col &lt; len(self.matrix[0]):<\/p>\n<p>            return self.matrix[row][col]<\/p>\n<p>        else:<\/p>\n<p>            print(&quot;\u7d22\u5f15\u8d85\u51fa\u8303\u56f4&quot;)<\/p>\n<p>            return None<\/p>\n<p>    def get_matrix(self):<\/p>\n<p>        return self.matrix<\/p>\n<h2><strong>\u793a\u4f8b<\/strong><\/h2>\n<p>matrix = Matrix(3, 3)<\/p>\n<p>matrix.set_value(0, 0, 1)<\/p>\n<p>matrix.set_value(1, 1, 2)<\/p>\n<p>matrix.set_value(2, 2, 3)<\/p>\n<p>print(matrix.get_matrix())  # \u8f93\u51fa: [[1, 0, 0], [0, 2, 0], [0, 0, 3]]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u9762\u7684\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u5b9a\u4e49\u4e86\u4e00\u4e2a<code>Matrix<\/code>\u7c7b\uff0c\u8be5\u7c7b\u7528\u4e8e\u7ba1\u7406\u4e8c\u7ef4\u77e9\u9635\u6570\u636e\u3002\u901a\u8fc7\u5728\u6784\u9020\u51fd\u6570\u4e2d\u521d\u59cb\u5316\u5d4c\u5957\u5217\u8868\uff0c\u5e76\u5b9a\u4e49\u65b9\u6cd5<code>set_value<\/code>\u3001<code>get_value<\/code>\u548c<code>get_matrix<\/code>\uff0c\u6211\u4eec\u53ef\u4ee5\u65b9\u4fbf\u5730\u8bbe\u7f6e\u77e9\u9635\u5143\u7d20\u3001\u83b7\u53d6\u77e9\u9635\u5143\u7d20\u548c\u83b7\u53d6\u6574\u4e2a\u77e9\u9635\u3002<\/p>\n<\/p>\n<p><h3>\u516b\u3001\u5728\u7c7b\u4e2d\u4f7f\u7528\u5217\u8868\u7684\u5176\u4ed6\u6280\u5de7<\/h3>\n<\/p>\n<p><p>\u5728\u7c7b\u4e2d\u4f7f\u7528\u5217\u8868\u65f6\uff0c\u8fd8\u6709\u4e00\u4e9b\u5e38\u89c1\u7684\u6280\u5de7\u548c\u6ce8\u610f\u4e8b\u9879\uff1a<\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>\u6df1\u62f7\u8d1d\u4e0e\u6d45\u62f7\u8d1d<\/strong>\uff1a\u5f53\u590d\u5236\u5217\u8868\u65f6\uff0c\u9ed8\u8ba4\u60c5\u51b5\u4e0b\u662f\u6d45\u62f7\u8d1d\uff0c\u5373\u65b0\u5217\u8868\u4e2d\u7684\u5143\u7d20\u662f\u5bf9\u539f\u5217\u8868\u4e2d\u5143\u7d20\u7684\u5f15\u7528\u3002\u5982\u679c\u9700\u8981\u72ec\u7acb\u7684\u526f\u672c\uff0c\u53ef\u4ee5\u4f7f\u7528\u6df1\u62f7\u8d1d\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import copy<\/p>\n<p>class MyClass:<\/p>\n<p>    def __init__(self):<\/p>\n<p>        self.my_list = [1, 2, 3]<\/p>\n<p>    def get_copy(self):<\/p>\n<p>        return copy.deepcopy(self.my_list)<\/p>\n<h2><strong>\u793a\u4f8b<\/strong><\/h2>\n<p>obj = MyClass()<\/p>\n<p>copy_list = obj.get_copy()<\/p>\n<p>copy_list[0] = 10<\/p>\n<p>print(obj.my_list)  # \u8f93\u51fa: [1, 2, 3]<\/p>\n<p>print(copy_list)  # \u8f93\u51fa: [10, 2, 3]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u5217\u8868\u63a8\u5bfc\u5f0f<\/strong>\uff1a\u5217\u8868\u63a8\u5bfc\u5f0f\u662f\u4e00\u79cd\u7b80\u6d01\u7684\u521b\u5efa\u5217\u8868\u7684\u65b9\u5f0f\uff0c\u53ef\u4ee5\u5728\u7c7b\u7684\u65b9\u6cd5\u4e2d\u4f7f\u7528\u5217\u8868\u63a8\u5bfc\u5f0f\u6765\u751f\u6210\u65b0\u5217\u8868\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">class MyClass:<\/p>\n<p>    def __init__(self):<\/p>\n<p>        self.my_list = [1, 2, 3, 4, 5]<\/p>\n<p>    def get_squared_list(self):<\/p>\n<p>        return [x  2 for x in self.my_list]<\/p>\n<h2><strong>\u793a\u4f8b<\/strong><\/h2>\n<p>obj = MyClass()<\/p>\n<p>print(obj.get_squared_list())  # \u8f93\u51fa: [1, 4, 9, 16, 25]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u5217\u8868\u6392\u5e8f<\/strong>\uff1a\u53ef\u4ee5\u4f7f\u7528<code>sort<\/code>\u65b9\u6cd5\u6216<code>sorted<\/code>\u51fd\u6570\u5bf9\u5217\u8868\u8fdb\u884c\u6392\u5e8f\u3002\u5728\u7c7b\u4e2d\u53ef\u4ee5\u5b9a\u4e49\u65b9\u6cd5\u6765\u6392\u5e8f\u5217\u8868\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">class MyClass:<\/p>\n<p>    def __init__(self):<\/p>\n<p>        self.my_list = [5, 2, 9, 1, 5, 6]<\/p>\n<p>    def sort_list(self):<\/p>\n<p>        self.my_list.sort()<\/p>\n<h2><strong>\u793a\u4f8b<\/strong><\/h2>\n<p>obj = MyClass()<\/p>\n<p>obj.sort_list()<\/p>\n<p>print(obj.my_list)  # \u8f93\u51fa: [1, 2, 5, 5, 6, 9]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<\/ol>\n<p><p>\u7efc\u4e0a\u6240\u8ff0\uff0c\u5728Python\u7c7b\u4e2d\u4f7f\u7528\u5217\u8868\u662f\u5b9e\u73b0\u6570\u636e\u7ba1\u7406\u548c\u903b\u8f91\u5904\u7406\u7684\u5e38\u89c1\u65b9\u5f0f\u3002\u901a\u8fc7\u521d\u59cb\u5316\u5217\u8868\u3001\u6dfb\u52a0\u5143\u7d20\u3001\u8bbf\u95ee\u5143\u7d20\u3001\u4fee\u6539\u5143\u7d20\u548c\u5220\u9664\u5143\u7d20\u7b49\u64cd\u4f5c\uff0c\u53ef\u4ee5\u5b9e\u73b0\u5bf9\u590d\u6742\u6570\u636e\u7ed3\u6784\u7684\u7ba1\u7406\u3002\u540c\u65f6\uff0c\u901a\u8fc7\u5b9e\u9645\u5e94\u7528\u548c\u4e00\u4e9b\u5e38\u89c1\u7684\u6280\u5de7\uff0c\u53ef\u4ee5\u8fdb\u4e00\u6b65\u63d0\u5347\u5217\u8868\u5728\u7c7b\u4e2d\u7684\u4f7f\u7528\u6548\u7387\u548c\u7075\u6d3b\u6027\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5728Python\u7c7b\u4e2d\u5982\u4f55\u5b9a\u4e49\u548c\u4f7f\u7528\u5217\u8868\uff1f<\/strong><br \/>\u5728Python\u7c7b\u4e2d\uff0c\u53ef\u4ee5\u901a\u8fc7\u5728\u7c7b\u7684\u6784\u9020\u51fd\u6570\uff08<code>__init__<\/code>\u65b9\u6cd5\uff09\u4e2d\u5b9a\u4e49\u5217\u8868\u5c5e\u6027\u6765\u521b\u5efa\u548c\u4f7f\u7528\u5217\u8868\u3002\u60a8\u53ef\u4ee5\u901a\u8fc7<code>self<\/code>\u5173\u952e\u5b57\u5c06\u5217\u8868\u4f5c\u4e3a\u5b9e\u4f8b\u53d8\u91cf\u8fdb\u884c\u521d\u59cb\u5316\uff0c\u4ece\u800c\u5728\u7c7b\u7684\u5176\u4ed6\u65b9\u6cd5\u4e2d\u8bbf\u95ee\u548c\u64cd\u4f5c\u8fd9\u4e2a\u5217\u8868\u3002\u4f8b\u5982\uff1a<\/p>\n<pre><code class=\"language-python\">class MyClass:\n    def __init__(self):\n        self.my_list = []\n\n    def add_item(self, item):\n        self.my_list.append(item)\n\n    def get_items(self):\n        return self.my_list\n<\/code><\/pre>\n<p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c<code>my_list<\/code>\u662f\u4e00\u4e2a\u7a7a\u5217\u8868\uff0c\u60a8\u53ef\u4ee5\u901a\u8fc7<code>add_item<\/code>\u65b9\u6cd5\u5411\u5217\u8868\u6dfb\u52a0\u5143\u7d20\uff0c\u5e76\u901a\u8fc7<code>get_items<\/code>\u65b9\u6cd5\u83b7\u53d6\u5217\u8868\u5185\u5bb9\u3002<\/p>\n<p><strong>\u5982\u4f55\u5728\u7c7b\u4e2d\u5bf9\u5217\u8868\u8fdb\u884c\u904d\u5386\u548c\u4fee\u6539\uff1f<\/strong><br \/>\u5728Python\u7c7b\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528\u5faa\u73af\u7ed3\u6784\u6765\u904d\u5386\u5217\u8868\uff0c\u5e76\u53ef\u4ee5\u901a\u8fc7\u7d22\u5f15\u6216\u65b9\u6cd5\u5bf9\u5217\u8868\u8fdb\u884c\u4fee\u6539\u3002\u4f8b\u5982\uff0c\u60a8\u53ef\u4ee5\u5728\u7c7b\u4e2d\u5b9a\u4e49\u4e00\u4e2a\u65b9\u6cd5\uff0c\u6765\u904d\u5386\u5217\u8868\u5e76\u8f93\u51fa\u6bcf\u4e2a\u5143\u7d20\uff1a<\/p>\n<pre><code class=\"language-python\">class MyClass:\n    def __init__(self):\n        self.my_list = [1, 2, 3]\n\n    def print_items(self):\n        for item in self.my_list:\n            print(item)\n\n    def update_item(self, index, value):\n        if 0 &lt;= index &lt; len(self.my_list):\n            self.my_list[index] = value\n<\/code><\/pre>\n<p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c<code>print_items<\/code>\u65b9\u6cd5\u904d\u5386\u5e76\u6253\u5370<code>my_list<\/code>\u4e2d\u7684\u6240\u6709\u5143\u7d20\uff0c\u800c<code>update_item<\/code>\u65b9\u6cd5\u5219\u5141\u8bb8\u60a8\u6839\u636e\u7d22\u5f15\u66f4\u65b0\u5217\u8868\u4e2d\u7684\u7279\u5b9a\u5143\u7d20\u3002<\/p>\n<p><strong>\u5728\u7c7b\u4e2d\u5982\u4f55\u4f7f\u7528\u5217\u8868\u4f5c\u4e3a\u53c2\u6570\uff1f<\/strong><br \/>\u60a8\u53ef\u4ee5\u5728\u7c7b\u7684\u65b9\u6cd5\u4e2d\u4f7f\u7528\u5217\u8868\u4f5c\u4e3a\u53c2\u6570\uff0c\u4ee5\u4fbf\u66f4\u7075\u6d3b\u5730\u64cd\u4f5c\u6570\u636e\u3002\u4f8b\u5982\uff0c\u53ef\u4ee5\u5b9a\u4e49\u4e00\u4e2a\u65b9\u6cd5\u6765\u63a5\u6536\u4e00\u4e2a\u5916\u90e8\u5217\u8868\u5e76\u5c06\u5176\u6dfb\u52a0\u5230\u7c7b\u5185\u90e8\u7684\u5217\u8868\u4e2d\uff1a<\/p>\n<pre><code class=\"language-python\">class MyClass:\n    def __init__(self):\n        self.my_list = []\n\n    def extend_list(self, items):\n        if isinstance(items, list):\n            self.my_list.extend(items)\n<\/code><\/pre>\n<p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c<code>extend_list<\/code>\u65b9\u6cd5\u63a5\u6536\u4e00\u4e2a\u5217\u8868\u4f5c\u4e3a\u53c2\u6570\uff0c\u5e76\u5c06\u5176\u5185\u5bb9\u6dfb\u52a0\u5230<code>my_list<\/code>\u4e2d\u3002\u8fd9\u6837\u53ef\u4ee5\u65b9\u4fbf\u5730\u5c06\u5176\u4ed6\u6570\u636e\u96c6\u5408\u5e76\u5230\u7c7b\u7684\u5185\u90e8\u5217\u8868\u4e2d\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u5728\u7c7b\u4e2d\u4f7f\u7528\u5217\u8868\uff0c\u901a\u8fc7\u521d\u59cb\u5316\u5217\u8868\u3001\u6dfb\u52a0\u5143\u7d20\u3001\u8bbf\u95ee\u5143\u7d20\u548c\u4fee\u6539\u5143\u7d20\u7b49\u64cd\u4f5c\u6765\u5b9e\u73b0\u590d\u6742\u7684\u6570\u636e\u7ed3\u6784\u7684 [&hellip;]","protected":false},"author":3,"featured_media":1069642,"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\/1069633"}],"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=1069633"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1069633\/revisions"}],"predecessor-version":[{"id":1069645,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1069633\/revisions\/1069645"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1069642"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1069633"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1069633"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1069633"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}