{"id":203,"date":"2023-02-22T22:20:42","date_gmt":"2023-02-22T14:20:42","guid":{"rendered":"https:\/\/www.appblog.cn\/?p=203"},"modified":"2023-04-30T15:29:20","modified_gmt":"2023-04-30T07:29:20","slug":"python-list","status":"publish","type":"post","link":"https:\/\/www.appblog.cn\/index.php\/2023\/02\/22\/python-list\/","title":{"rendered":"Python\u5217\u8868"},"content":{"rendered":"<h2>\u5217\u8868<\/h2>\n<h3>\u4e00\u4e2a\u6570\u7ec4\u4e2d\u53ef\u4ee5\u6709\u5b57\u7b26\u4e32\uff0c\u4e5f\u53ef\u4ee5\u6709\u6570\u5b57<\/h3>\n<p><!-- more --><\/p>\n<pre><code class=\"language-python\">>&gt;&gt; spam = [[&#039;cat&#039;,&#039;bat&#039;],[10,20,30]]\n>&gt;&gt; spam[1]\n[10, 20, 30]\n>&gt;&gt; spam[0]\n[&#039;cat&#039;, &#039;bat&#039;]\n>&gt;&gt; spam[0][0]\n&#039;cat&#039;\n>&gt;&gt; spam[1][0]\n10\n>&gt;&gt; spam[0][0][0]\n&#039;c&#039;\n>&gt;&gt; spam[-1]\n[10, 20, 30]<\/code><\/pre>\n<h3>\u5229\u7528\u5207\u7247\u83b7\u53d6\u5b50\u5217\u8868<\/h3>\n<pre><code class=\"language-python\">>&gt;&gt; spam\n[&#039;a&#039;, &#039;b&#039;, &#039;c&#039;, &#039;d&#039;]\n>&gt;&gt; spam[1:3]\n[&#039;b&#039;, &#039;c&#039;]\n>&gt;&gt; spam[1:-1]\n[&#039;b&#039;, &#039;c&#039;]\n>&gt;&gt; <\/code><\/pre>\n<h2>\u5217\u8868\u51fd\u6570<\/h2>\n<h3>len<\/h3>\n<pre><code class=\"language-python\">>&gt;&gt; len(spam)\n4<\/code><\/pre>\n<h3>\u64cd\u4f5c\u7b26 \u2018*\u2019,\u2019+\u2019\u8fd0\u7b97<\/h3>\n<pre><code class=\"language-python\">>&gt;&gt; [&#039;a&#039;,&#039;c&#039;,&#039;d&#039;]+[1,2,3,4]\n[&#039;a&#039;, &#039;c&#039;, &#039;d&#039;, 1, 2, 3, 4]\n>&gt;&gt; ([&#039;a&#039;,&#039;c&#039;,&#039;d&#039;]+[1,2,3,4])*3\n[&#039;a&#039;, &#039;c&#039;, &#039;d&#039;, 1, 2, 3, 4, &#039;a&#039;, &#039;c&#039;, &#039;d&#039;, 1, 2, 3, 4, &#039;a&#039;, &#039;c&#039;, &#039;d&#039;, 1, 2, 3, 4]<\/code><\/pre>\n<h3>del\u4ece\u5217\u8868\u4e2d\u5220\u9664<\/h3>\n<pre><code class=\"language-python\">>&gt;&gt; spam\n[&#039;a&#039;, &#039;b&#039;, &#039;c&#039;, &#039;d&#039;]\n>&gt;&gt; del spam[1]\n>&gt;&gt; spam\n[&#039;a&#039;, &#039;c&#039;, &#039;d&#039;]<\/code><\/pre>\n<h3>for\u5faa\u73af\u548c\u5217\u8868<\/h3>\n<pre><code class=\"language-python\">>&gt;&gt; for i in [1,2,3]:\n    print(spam[i])<\/code><\/pre>\n<h3>in \u548c not in \u64cd\u4f5c<\/h3>\n<pre><code class=\"language-python\">>&gt;&gt; spam = [&#039;a&#039;, &#039;b&#039;, &#039;c&#039;, &#039;d&#039;]\n>&gt;&gt; spam\n[&#039;a&#039;, &#039;b&#039;, &#039;c&#039;, &#039;d&#039;]\n>&gt;&gt; a = &#039;a&#039;\n>&gt;&gt; a in spam\nTrue\n>&gt;&gt; temp = &#039;f&#039;\n>&gt;&gt; temp not in spam\nTrue<\/code><\/pre>\n<h2>\u591a\u91cd\u8d4b\u503c\u6280\u5de7<\/h2>\n<pre><code class=\"language-python\">>&gt;&gt; spam = [&#039;a&#039;, &#039;b&#039;, &#039;c&#039;, &#039;d&#039;]\n>&gt;&gt; a,b,c,d = spam\n>&gt;&gt; a\n&#039;a&#039;\n>&gt;&gt; spam[2:4] = [&#039;1&#039;, &#039;2&#039;]\n>&gt;&gt; spam\n[&#039;a&#039;, &#039;b&#039;, &#039;1&#039;, &#039;2&#039;]<\/code><\/pre>\n<h2>\u65b9\u6cd5<\/h2>\n<h3>index()\u65b9\u6cd5\u5728\u5217\u8868\u4e2d\u67e5\u627e\u503c<\/h3>\n<pre><code class=\"language-python\">>&gt;&gt; spam = [&#039;a&#039;,&#039;b&#039;,&#039;c&#039;]\n>&gt;&gt; spam\n[&#039;a&#039;, &#039;b&#039;, &#039;c&#039;]\n>&gt;&gt; spam.index(&#039;a&#039;)\n0<\/code><\/pre>\n<h3>append()\u548cinsert()\u7684\u4f7f\u7528<\/h3>\n<pre><code class=\"language-python\">>&gt;&gt; spam\n[&#039;a&#039;, &#039;b&#039;, &#039;c&#039;]\n>&gt;&gt; spam.append(a)\n>&gt;&gt; spam\n[&#039;a&#039;, &#039;b&#039;, &#039;c&#039;, &#039;a&#039;]  #\u5728\u5217\u8868\u6700\u540e\u6dfb\u52a0\u9879\u76ee\n>&gt;&gt; spam.insert(1, &#039;temp&#039;)\n>&gt;&gt; spam\n[&#039;a&#039;, &#039;temp&#039;, &#039;b&#039;, &#039;c&#039;, &#039;a&#039;]  #\u5728\u6307\u5b9a\u4f4d\u7f6e\u6dfb\u52a0\u5236\u5b9a\u9879<\/code><\/pre>\n<h3>remove()\u65b9\u6cd5\u4ece\u5217\u8868\u4e2d\u5220\u9664\u503c<\/h3>\n<pre><code class=\"language-python\">>&gt;&gt; spam\n[&#039;a&#039;, &#039;temp&#039;, &#039;b&#039;, &#039;c&#039;, &#039;a&#039;]\n>&gt;&gt; spam.remove(&#039;temp&#039;)\n>&gt;&gt; spam\n[&#039;a&#039;, &#039;b&#039;, &#039;c&#039;, &#039;a&#039;]\n>&gt;&gt; spam.remove(&#039;g&#039;)\nTraceback (most recent call last):\n  File &quot;&lt;pyshell#86&gt;&quot;, line 1, in &lt;module&gt;\n    spam.remove(&#039;g&#039;)\nValueError: list.remove(x): x not in list<\/code><\/pre>\n<h3>sort()\u5c06\u5217\u8868\u4e2d\u7684\u503c\u7684\u6392\u5e8f<\/h3>\n<p>\u9ed8\u8ba4\u6309\u7167ASCII\u6392\u5e8f<\/p>\n<pre><code class=\"language-python\">>&gt;&gt; s = [&#039;c&#039;, &#039;b&#039;, &#039;a&#039;, 1, 5, 3, 3.1]\n>&gt;&gt; s.sort()\n>&gt;&gt; s\n[1, 3, 3.1, 5, &#039;a&#039;, &#039;b&#039;, &#039;c&#039;]<\/code><\/pre>\n<h3>\u9006\u5411\u6392\u5e8f<\/h3>\n<pre><code class=\"language-python\">>&gt;&gt; s = [1, 4, 3.14, 3, 5]\n>&gt;&gt; s.sort(reverse=True)\n>&gt;&gt; s\n[5, 4, 3.14, 3, 1]<\/code><\/pre>\n<h3>\u6309\u7167\u5b57\u5178\u6392\u5e8f<\/h3>\n<p>\u6ce8\uff1akey = str.lower \u662f\u628a\u6240\u6709\u5143\u7d20\u5f53\u4f5c\u5c0f\u5199\u00b7<\/p>\n<pre><code>>&gt;&gt; spam = [&#039;a&#039;,&#039;Z&#039;,&#039;z&#039;,&#039;A&#039;]\n>&gt;&gt; spam.sort(key=str.lower)\n>&gt;&gt; spam\n[&#039;a&#039;, &#039;A&#039;, &#039;Z&#039;, &#039;z&#039;]<\/code><\/pre>\n<h2>\u5143\u7ec4\u6570\u636e\u7c7b\u578b<\/h2>\n<p>\u5143\u7ec4\u7684\u5185\u5bb9\u662f\u4e0d\u53ef\u6539\u53d8\u7684\uff0c\u5176\u6b21Python\u5bf9\u5143\u7ec4\u8fdb\u884c\u4e86\u4f18\u5316\uff0c\u4f7f\u7528\u901f\u5ea6\u66f4\u5feb<\/p>\n<pre><code class=\"language-python\">>&gt;&gt; sp = (&#039;hello&#039;,99,3.14)\n>&gt;&gt; type(sp)\n&lt;type &#039;tuple&#039;&gt;\n>&gt;&gt; type((&#039;hello&#039;,))\n&lt;class &#039;tuple&#039;&gt;\n>&gt;&gt; type((&#039;hello&#039;))\n&lt;class &#039;str&#039;&gt;\n>&gt;&gt; sp[1]\n99\n>&gt;&gt; sp[1] = 88\nTraceback (most recent call last):\n  File &quot;&lt;stdin&gt;&quot;, line 1, in &lt;module&gt;\nTypeError: &#039;tuple&#039; object does not support item assignment<\/code><\/pre>\n<h2>\u4f7f\u7528list()\u548ctuple()\u51fd\u6570\u6765\u8f6c\u6362\u7c7b\u578b<\/h2>\n<pre><code class=\"language-python\">>&gt;&gt; list((&#039;who&#039;,&#039;are&#039;,&#039;you&#039;))\n[&#039;who&#039;, &#039;are&#039;, &#039;you&#039;]\n>&gt;&gt; list((&#039;hello&#039;))\n[&#039;h&#039;, &#039;e&#039;, &#039;l&#039;, &#039;l&#039;, &#039;o&#039;]\n>&gt;&gt; tuple([&#039;hello&#039;,&#039;word&#039;])\n(&#039;hello&#039;, &#039;word&#039;)\n>&gt;&gt; tuple([&#039;hello&#039;])\n(&#039;hello&#039;,)<\/code><\/pre>\n<h2>\u5f15\u7528<\/h2>\n<p>\u5728\u8d4b\u503c\u65f6\u5e76\u672a\u590d\u5236\uff0c\u800c\u662f\u6307\u5411\u540c\u4e00\u4e2a\u5217\u8868<\/p>\n<pre><code class=\"language-python\">>&gt;&gt; spam\n[1, 5, 4, 2, &#039;hello&#039;, &#039;Z&#039;, &#039;z&#039;]\n>&gt;&gt; chees = spam\n>&gt;&gt; chees[2] = &#039;world&#039;\n>&gt;&gt; spam\n[1, 5, &#039;world&#039;, 2, &#039;hello&#039;, &#039;Z&#039;, &#039;z&#039;]\n>&gt;&gt; chees\n[1, 5, &#039;world&#039;, 2, &#039;hello&#039;, &#039;Z&#039;, &#039;z&#039;]<\/code><\/pre>\n<h2>\u4f20\u9012\u5e94\u7528\uff08\u7c7b\u4f3c\u4e8e\u5f15\u7528\u8c03\u7528\uff09<\/h2>\n<pre><code class=\"language-python\">def eggs(temp):\n    temp.append(&#039;hello&#039;)\nspam = [1,2,3]\neggs(spam)\nprint(spam)\n#[1, 2, 3, &#039;hello&#039;]<\/code><\/pre>\n<h2>copy()\u51fd\u6570\u548cdeepcopy()\u51fd\u6570<\/h2>\n<p>copy.copy() \u53ef\u4ee5\u7528\u6765\u590d\u5236\u5217\u8868\u548c\u5b57\u5178\u8fd9\u6837\u7684\u53ef\u53d8\u503c\uff0c\u800c\u4e0d\u53ea\u662f\u590d\u5236\u5f15\u7528<\/p>\n<pre><code class=\"language-python\">>&gt;&gt; import copy\n>&gt;&gt; spam = [1,2,3]\n>&gt;&gt; spam\n[1, 2, 3]\n>&gt;&gt; temp = copy.copy(spam)\n>&gt;&gt; temp\n[1, 2, 3]\n>&gt;&gt; spam.append(&#039;hello&#039;)\n>&gt;&gt; spam\n[1, 2, 3, &#039;hello&#039;]\n>&gt;&gt; temp\n[1, 2, 3]\n>&gt;&gt; temp.append(&#039;OK&#039;)\n>&gt;&gt; spam,temp\n([1, 2, 3, &#039;hello&#039;], [1, 2, 3, &#039;OK&#039;])<\/code><\/pre>\n<p>\u5982\u679c\u8bf4\u5217\u8868\u4e2d\u5305\u542b\u4e86\u5217\u8868\uff0c\u5219\u5e94\u5f53\u4f7f\u7528copy.deepcopt()<\/p>\n<pre><code class=\"language-python\">>&gt;&gt; spam\n[[1, 2, 3, &#039;OK&#039;], 1, 2]\n>&gt;&gt; temp = copy.deepcopy(spam)\n>&gt;&gt; temp\n[[1, 2, 3, &#039;OK&#039;], 1, 2]<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u5217\u8868 \u4e00\u4e2a\u6570\u7ec4\u4e2d\u53ef\u4ee5\u6709\u5b57\u7b26\u4e32\uff0c\u4e5f\u53ef\u4ee5\u6709\u6570\u5b57 >&gt;&gt; spam = [[&#039;cat&#038;#039 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[79],"tags":[],"class_list":["post-203","post","type-post","status-publish","format-standard","hentry","category-python"],"_links":{"self":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts\/203","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/comments?post=203"}],"version-history":[{"count":0,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts\/203\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/media?parent=203"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/categories?post=203"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/tags?post=203"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}