{"id":196,"date":"2023-10-26T16:32:31","date_gmt":"2023-10-26T08:32:31","guid":{"rendered":"http:\/\/ai.gitpp.com\/?p=196"},"modified":"2023-10-27T09:34:26","modified_gmt":"2023-10-27T01:34:26","slug":"5-%e6%95%b0%e6%8d%ae%e7%bb%93%e6%9e%84","status":"publish","type":"post","link":"http:\/\/ai.gitpp.com\/index.php\/2023\/10\/26\/5-%e6%95%b0%e6%8d%ae%e7%bb%93%e6%9e%84\/","title":{"rendered":"\u300aPython\u5165\u95e8\u6559\u7a0b\u300b5. \u6570\u636e\u7ed3\u6784"},"content":{"rendered":"\n<ol class=\"wp-block-list\">\n<li>\u5f00\u80c3\u83dc<\/li>\n\n\n\n<li><a href=\"http:\/\/ai.gitpp.com\/index.php\/2023\/10\/26\/2-%e4%bd%bf%e7%94%a8-python-%e8%a7%a3%e9%87%8a%e5%99%a8\/\">\u4f7f\u7528 Python \u89e3\u91ca\u5668<\/a><\/li>\n\n\n\n<li><a href=\"http:\/\/ai.gitpp.com\/index.php\/2023\/10\/26\/3-%e9%9d%9e%e6%ad%a3%e5%bc%8f%e4%bb%8b%e7%bb%8dpython\/\">\u975e\u6b63\u5f0f\u4ecb\u7ecdPython<\/a><\/li>\n\n\n\n<li><a href=\"http:\/\/ai.gitpp.com\/index.php\/2023\/10\/26\/4-%e6%b7%b1%e5%85%a5%e6%b5%81%e7%a8%8b%e6%8e%a7%e5%88%b6\/\">\u6df1\u5165\u6d41\u7a0b\u63a7\u5236<\/a><\/li>\n\n\n\n<li><a href=\"http:\/\/ai.gitpp.com\/index.php\/2023\/10\/26\/5-%e6%95%b0%e6%8d%ae%e7%bb%93%e6%9e%84\/\">\u6570\u636e\u7ed3\u6784<\/a><\/li>\n\n\n\n<li><a href=\"http:\/\/ai.gitpp.com\/index.php\/2023\/10\/26\/6-%e6%a8%a1%e5%9d%97\/\">\u6a21\u5757<\/a><\/li>\n\n\n\n<li><a href=\"http:\/\/ai.gitpp.com\/index.php\/2023\/10\/26\/7-%e8%be%93%e5%85%a5%e5%92%8c%e8%be%93%e5%87%ba\/\">\u8f93\u5165\u548c\u8f93\u51fa<\/a><\/li>\n\n\n\n<li><a href=\"http:\/\/ai.gitpp.com\/index.php\/2023\/10\/26\/8-%e9%94%99%e8%af%af%e5%92%8c%e5%bc%82%e5%b8%b8\/\">\u9519\u8bef\u548c\u5f02\u5e38<\/a><\/li>\n\n\n\n<li><a href=\"http:\/\/ai.gitpp.com\/index.php\/2023\/10\/26\/9-%e7%b1%bb\/\">\u7c7b<\/a><\/li>\n\n\n\n<li><a href=\"http:\/\/ai.gitpp.com\/index.php\/2023\/10\/26\/10-%e6%a0%87%e5%87%86%e5%ba%93%e7%9a%84%e7%ae%80%e6%98%8e%e4%bb%8b%e7%bb%8d\/\">\u6807\u51c6\u5e93\u7684\u7b80\u660e\u4ecb\u7ecd<\/a><\/li>\n\n\n\n<li><a href=\"http:\/\/ai.gitpp.com\/index.php\/2023\/10\/26\/11-%e6%a0%87%e5%87%86%e5%ba%93%e7%9a%84%e7%ae%80%e6%98%8e%e4%bb%8b%e7%bb%8d%ef%bc%88%e7%ac%ac%e4%ba%8c%e9%83%a8%e5%88%86%ef%bc%89\/\">\u6807\u51c6\u5e93\u7684\u7b80\u660e\u4ecb\u7ecd\uff08\u7b2c\u4e8c\u90e8\u5206\uff09<\/a><\/li>\n\n\n\n<li><a href=\"http:\/\/ai.gitpp.com\/index.php\/2023\/10\/26\/12-%e7%8e%b0%e5%9c%a8%e5%b9%b2%e4%bb%80%e4%b9%88\/\">\u73b0\u5728\u5e72\u4ec0\u4e48?<\/a><\/li>\n\n\n\n<li><a href=\"http:\/\/ai.gitpp.com\/index.php\/2023\/10\/26\/13-%e4%ba%a4%e4%ba%92%e5%bc%8f%e8%be%93%e5%85%a5%e7%bc%96%e8%be%91%e5%8f%8a%e5%8e%86%e5%8f%b2%e6%9b%bf%e4%bb%a3\/\">\u4ea4\u4e92\u5f0f\u8f93\u5165\u7f16\u8f91\u53ca\u5386\u53f2\u66ff\u4ee3<\/a><\/li>\n\n\n\n<li><a href=\"http:\/\/ai.gitpp.com\/index.php\/2023\/10\/26\/14-%e6%b5%ae%e7%82%b9%e7%ae%97%e6%9c%af-%e9%97%ae%e9%a2%98%e5%92%8c%e9%99%90%e5%88%b6\/\">\u6d6e\u70b9\u7b97\u672f: \u95ee\u9898\u548c\u9650\u5236<\/a><\/li>\n<\/ol>\n\n\n\n<p><em># \u5269\u4f59\u7684\u961f\u5217, \u5b83\u6309\u7167\u8fdb\u5165\u7684\u987a\u5e8f\u6392\u5217<\/em> deque([&#8216;Michael&#8217;, &#8216;Terry&#8217;, &#8216;Graham&#8217;])<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">5.1.3. \u5217\u8868\u63a8\u5bfc\u5f0f<a href=\"https:\/\/docspy3zh.readthedocs.io\/en\/latest\/tutorial\/datastructures.html#tut-listcomps\"><\/a><\/h3>\n\n\n\n<p>\u5217\u8868\u63a8\u5bfc\u5f0f\u63d0\u4f9b\u4e86\u4ece\u5e8f\u5217\u4e2d\u521b\u5efa\u5217\u8868\u7684\u7b80\u4fbf\u9014\u5f84. \u901a\u5e38\u7a0b\u5e8f\u4f1a\u5bf9\u5e8f\u5217\u7684\u6bcf\u4e00\u4e2a\u5143\u7d20\u505a\u4e9b\u64cd\u4f5c,\u5e76\u4ee5\u5176\u7ed3\u679c\u4f5c\u4e3a\u65b0\u5217\u8868\u7684\u5143\u7d20, \u6216\u8005\u6839\u636e\u6307\u5b9a\u7684\u6761\u4ef6\u6765\u521b\u5efa\u5b50\u5e8f\u5217.<\/p>\n\n\n\n<p>\u800c\u5217\u8868\u63a8\u5bfc\u5f0f\u7684\u7ed3\u6784\u662f, \u5728\u4e00\u4e2a\u65b9\u62ec\u53f7\u91cc, \u9996\u5148\u662f\u4e00\u4e2a\u8868\u8fbe\u5f0f, \u968f\u540e\u662f\u4e00\u4e2a&nbsp;<a href=\"https:\/\/docspy3zh.readthedocs.io\/en\/latest\/reference\/compound_stmts.html#for\"><code>for<\/code><\/a>&nbsp;\u5b50\u53e5, \u7136\u540e\u662f\u96f6\u4e2a\u6216\u66f4\u591a\u7684&nbsp;<a href=\"https:\/\/docspy3zh.readthedocs.io\/en\/latest\/reference\/compound_stmts.html#for\"><code>for<\/code><\/a>&nbsp;\u6216&nbsp;<a href=\"https:\/\/docspy3zh.readthedocs.io\/en\/latest\/reference\/compound_stmts.html#if\"><code>if<\/code><\/a>&nbsp;\u5b50\u53e5. \u7ed3\u679c\u5c06\u662f\u901a\u8fc7\u8ba1\u7b97&nbsp;<a href=\"https:\/\/docspy3zh.readthedocs.io\/en\/latest\/reference\/compound_stmts.html#for\"><code>for<\/code><\/a>&nbsp;\u548c&nbsp;<a href=\"https:\/\/docspy3zh.readthedocs.io\/en\/latest\/reference\/compound_stmts.html#if\"><code>if<\/code><\/a>&nbsp;\u5b50\u53e5\u6765\u83b7\u5f97\u7684\u4e00\u4e2a\u5217\u8868. \u5982\u679c\u8981\u4f7f\u8868\u8fbe\u5f0f\u63a8\u5bfc\u5f0f\u51fa\u5143\u7ec4, \u5c31\u5fc5\u987b\u7528\u5706\u62ec\u53f7.<\/p>\n\n\n\n<p>\u8fd9\u91cc\u6211\u4eec\u5c06\u4e00\u4e2a\u6570\u5b57\u5217\u8868\u6bcf\u4e2a\u5143\u7d20\u7ffb\u4e09\u500d\u4ece\u800c\u751f\u6210\u4e00\u4e2a\u65b0\u5217\u8868:<strong>&gt;&gt;&gt; <\/strong>vec = [2, 4, 6] <strong>&gt;&gt;&gt; <\/strong>[3*x <strong>for<\/strong> x <strong>in<\/strong> vec] [6, 12, 18]<\/p>\n\n\n\n<p>\u73b0\u5728\u52a0\u70b9\u513f\u5c0f\u82b1\u6837:<strong>&gt;&gt;&gt; <\/strong>[[x, x**2] <strong>for<\/strong> x <strong>in<\/strong> vec] [[2, 4], [4, 16], [6, 36]]<\/p>\n\n\n\n<p>\u8fd9\u91cc\u6211\u4eec\u5bf9\u5e8f\u5217\u91cc\u6bcf\u4e00\u9879\u9010\u4e2a\u8c03\u7528\u67d0\u65b9\u6cd5:<strong>&gt;&gt;&gt; <\/strong>freshfruit = [&#8216; banana&#8217;, &#8216; loganberry &#8216;, &#8216;passion fruit &#8216;] <strong>&gt;&gt;&gt; <\/strong>[weapon.strip() <strong>for<\/strong> weapon <strong>in<\/strong> freshfruit] [&#8216;banana&#8217;, &#8216;loganberry&#8217;, &#8216;passion fruit&#8217;]<\/p>\n\n\n\n<p>\u6211\u4eec\u53ef\u4ee5\u7528&nbsp;<a href=\"https:\/\/docspy3zh.readthedocs.io\/en\/latest\/reference\/compound_stmts.html#if\"><code>if<\/code><\/a>&nbsp;\u5b50\u53e5\u6765\u8fdb\u884c\u8fc7\u6ee4:<strong>&gt;&gt;&gt; <\/strong>[3*x <strong>for<\/strong> x <strong>in<\/strong> vec <strong>if<\/strong> x &gt; 3] [12, 18] <strong>&gt;&gt;&gt; <\/strong>[3*x <strong>for<\/strong> x <strong>in<\/strong> vec <strong>if<\/strong> x &lt; 2] []<\/p>\n\n\n\n<p>\u5143\u7ec4\u7ecf\u5e38\u80fd\u4e0d\u7528\u5706\u62ec\u53f7\u800c\u521b\u5efa, \u4f46\u8fd9\u91cc\u4e0d\u884c:<strong>&gt;&gt;&gt; <\/strong>[x, x**2 <strong>for<\/strong> x <strong>in<\/strong> vec] <em># error &#8211; parens required for tuples<\/em> File &#8220;&lt;stdin&gt;&#8221;, line 1, in ? [x, x**2 for x in vec] ^ SyntaxError: invalid syntax <strong>&gt;&gt;&gt; <\/strong>[(x, x**2) <strong>for<\/strong> x <strong>in<\/strong> vec] [(2, 4), (4, 16), (6, 36)]<\/p>\n\n\n\n<p>\u8fd9\u91cc\u662f\u4e00\u4e9b\u5faa\u73af\u7684\u5d4c\u5957\u548c\u5176\u5b83\u6280\u5de7\u7684\u6f14\u793a:<strong>&gt;&gt;&gt; <\/strong>vec1 = [2, 4, 6] <strong>&gt;&gt;&gt; <\/strong>vec2 = [4, 3, -9] <strong>&gt;&gt;&gt; <\/strong>[x*y <strong>for<\/strong> x <strong>in<\/strong> vec1 <strong>for<\/strong> y <strong>in<\/strong> vec2] [8, 6, -18, 16, 12, -36, 24, 18, -54] <strong>&gt;&gt;&gt; <\/strong>[x+y <strong>for<\/strong> x <strong>in<\/strong> vec1 <strong>for<\/strong> y <strong>in<\/strong> vec2] [6, 5, -7, 8, 7, -5, 10, 9, -3] <strong>&gt;&gt;&gt; <\/strong>[vec1[i]*vec2[i] <strong>for<\/strong> i <strong>in<\/strong> range(len(vec1))] [8, 12, -54]<\/p>\n\n\n\n<p>\u5217\u8868\u63a8\u5bfc\u5f0f\u53ef\u4f7f\u7528\u590d\u6742\u7684\u8868\u8fbe\u5f0f\u548c\u5d4c\u5957\u7684\u51fd\u5f0f:<strong>&gt;&gt;&gt; <\/strong>[str(round(355\/113, i)) <strong>for<\/strong> i <strong>in<\/strong> range(1, 6)] [&#8216;3.1&#8217;, &#8216;3.14&#8217;, &#8216;3.142&#8217;, &#8216;3.1416&#8217;, &#8216;3.14159&#8217;]<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">5.1.4. \u5d4c\u5957\u5217\u8868\u63a8\u5bfc\u5f0f<a href=\"https:\/\/docspy3zh.readthedocs.io\/en\/latest\/tutorial\/datastructures.html#id12\"><\/a><\/h3>\n\n\n\n<p>\u5982\u679c\u4f60\u53d7\u7684\u4e86\u7684\u8bdd, \u5176\u5b9e\u5217\u8868\u63a8\u5bfc\u5f0f\u662f\u53ef\u4ee5\u5d4c\u5957\u7684. \u5b83\u7684\u786e\u662f\u4e2a\u5f3a\u5927\u7684\u5de5\u5177, \u4f46 \u2013 \u5c31\u50cf\u6240\u6709\u5f3a\u5927\u7684\u5de5\u5177\u4e00\u6837 \u2013 \u9700\u8981\u88ab\u5c0f\u5fc3\u5730\u4f7f\u7528,<\/p>\n\n\n\n<p>\u8003\u8651\u4e0b\u9762\u7684\u4f8b\u5b50, \u6709\u4e00\u4e2a 3&#215;3 \u7684\u77e9\u9635, \u5b58\u50a8\u4e3a\u4e00\u4e2a\u5305\u542b\u4e09\u4e2a\u5217\u8868\u7684\u5217\u8868, \u6bcf\u4e00\u884c\u4e00\u4e2a\u5217\u8868:<strong>&gt;&gt;&gt; <\/strong>mat = [ <strong>&#8230;<\/strong> [1, 2, 3], <strong>&#8230;<\/strong> [4, 5, 6], <strong>&#8230;<\/strong> [7, 8, 9], <strong>&#8230;<\/strong> ]<\/p>\n\n\n\n<p>\u73b0\u5728, \u5982\u679c\u4f60\u60f3\u4ea4\u6362\u884c\u548c\u5217, \u53ef\u4ee5\u4f7f\u7528\u5217\u8868\u63a8\u5bfc\u5f0f:<strong>&gt;&gt;&gt; <\/strong>print([[row[i] <strong>for<\/strong> row <strong>in<\/strong> mat] <strong>for<\/strong> i <strong>in<\/strong> [0, 1, 2]]) [[1, 4, 7], [2, 5, 8], [3, 6, 9]]<\/p>\n\n\n\n<p>\u4f7f\u7528*\u5d4c\u5957*\u5217\u8868\u63a8\u5bfc\u5f0f\u65f6\u7279\u522b\u9700\u8981\u6ce8\u610f:<\/p>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<p>\u4ece\u53f3\u81f3\u5de6\u5730\u9605\u8bfb\u5d4c\u5957\u5217\u8868\u63a8\u5bfc\u5f0f\u66f4\u5bb9\u6613\u7406\u89e3.<\/p>\n<\/blockquote>\n\n\n\n<p>\u8be5\u4ee3\u7801\u7684\u5197\u957f\u7248\u672c, \u5c31\u660e\u767d\u5730\u8868\u8ff0\u4e86\u6d41\u7a0b:<strong>for<\/strong> i <strong>in<\/strong> [0, 1, 2]: <strong>for<\/strong> row <strong>in<\/strong> mat: print(row[i], end=&#8221;&#8221;) print()<\/p>\n\n\n\n<p>\u73b0\u5b9e\u4e2d, \u4f60\u5e94\u5f53\u9009\u62e9\u5185\u5efa\u51fd\u5f0f\u6765\u5904\u7406\u590d\u6742\u6d41\u7a0b. \u8fd9\u91cc, \u51fd\u5f0f&nbsp;<a href=\"https:\/\/docspy3zh.readthedocs.io\/en\/latest\/library\/functions.html#zip\"><code>zip()<\/code><\/a>&nbsp;\u5c31\u975e\u5e38\u597d\u7528.<strong>&gt;&gt;&gt; <\/strong>list(zip(*mat)) [(1, 4, 7), (2, 5, 8), (3, 6, 9)]<\/p>\n\n\n\n<p>\u53c2\u89c1&nbsp;<a href=\"https:\/\/docspy3zh.readthedocs.io\/en\/latest\/tutorial\/controlflow.html#tut-unpacking-arguments\">\u53c2\u6570\u5217\u8868\u89e3\u5305<\/a>&nbsp;\u4e86\u89e3\u672c\u884c\u4e2d\u661f\u53f7\u7684\u8be6\u7ec6\u5185\u5bb9.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">5.2.&nbsp;<a href=\"https:\/\/docspy3zh.readthedocs.io\/en\/latest\/reference\/simple_stmts.html#del\"><code>del<\/code><\/a>&nbsp;\u8bed\u53e5<a href=\"https:\/\/docspy3zh.readthedocs.io\/en\/latest\/tutorial\/datastructures.html#del\"><\/a><\/h2>\n\n\n\n<p>\u8fd9\u6709\u4e00\u79cd\u901a\u8fc7\u7ed9\u5b9a\u7d22\u5f15\u800c\u4e0d\u662f\u503c, \u6765\u5220\u9664\u5217\u8868\u4e2d\u9879\u7684\u65b9\u6cd5: \u7528&nbsp;<a href=\"https:\/\/docspy3zh.readthedocs.io\/en\/latest\/reference\/simple_stmts.html#del\"><code>del<\/code><\/a>&nbsp;\u8bed\u53e5. \u5b83\u4e0e\u8fd4\u56de\u4e00\u4e2a\u503c\u7684&nbsp;<code>pop()<\/code>&nbsp;\u65b9\u6cd5\u4e0d\u540c.&nbsp;<a href=\"https:\/\/docspy3zh.readthedocs.io\/en\/latest\/reference\/simple_stmts.html#del\"><code>del<\/code><\/a>&nbsp;\u8bed\u53e5\u4e5f\u53ef\u4ee5\u79fb\u9664\u5217\u8868\u4e2d\u7684\u5207\u7247, \u6216\u8005\u6e05\u9664\u6574\u4e2a\u5217\u8868 (\u4e4b\u524d\u6211\u4eec\u901a\u8fc7\u7ed9\u5207\u7247\u8d4b\u503c\u4e3a\u7a7a\u5217\u8868\u6765\u5b8c\u6210\u8fd9\u70b9). \u4f8b\u5982:<strong>&gt;&gt;&gt; <\/strong>a = [-1, 1, 66.25, 333, 333, 1234.5] <strong>&gt;&gt;&gt; <\/strong><strong>del<\/strong> a[0] <strong>&gt;&gt;&gt; <\/strong>a [1, 66.25, 333, 333, 1234.5] <strong>&gt;&gt;&gt; <\/strong><strong>del<\/strong> a[2:4] <strong>&gt;&gt;&gt; <\/strong>a [1, 66.25, 1234.5] <strong>&gt;&gt;&gt; <\/strong><strong>del<\/strong> a[:] <strong>&gt;&gt;&gt; <\/strong>a []<\/p>\n\n\n\n<p><a href=\"https:\/\/docspy3zh.readthedocs.io\/en\/latest\/reference\/simple_stmts.html#del\"><code>del<\/code><\/a>&nbsp;\u4e5f\u53ef\u4ee5\u7528\u4e8e\u5220\u9664\u53d8\u91cf\u5b9e\u4f53:<strong>&gt;&gt;&gt; <\/strong><strong>del<\/strong> a<\/p>\n\n\n\n<p>\u5728\u8fd9\u4e4b\u540e\u5f15\u7528&nbsp;<code>a<\/code>&nbsp;\u7684\u8bdd\u4f1a\u4ea7\u751f\u4e00\u4e2a\u9519\u8bef (\u81f3\u5c11\u5230\u7ed9\u5b83\u8d4b\u53e6\u4e00\u4e2a\u503c\u4e4b\u524d). \u6211\u4eec\u5c06\u5728\u540e\u9762\u627e\u5230&nbsp;<a href=\"https:\/\/docspy3zh.readthedocs.io\/en\/latest\/reference\/simple_stmts.html#del\"><code>del<\/code><\/a>&nbsp;\u7684\u5176\u5b83\u7528\u6cd5.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">5.3. \u5143\u7ec4\u548c\u5e8f\u5217<a href=\"https:\/\/docspy3zh.readthedocs.io\/en\/latest\/tutorial\/datastructures.html#tut-tuples\"><\/a><\/h2>\n\n\n\n<p>\u6211\u4eec\u770b\u5230\u5217\u8868\u548c\u5b57\u4e32\u6709\u5f88\u591a\u901a\u7528\u7684\u5c5e\u6027, \u4f8b\u5982\u7d22\u5f15\u548c\u5207\u7247\u64cd\u4f5c. \u5b83\u4eec\u662f&nbsp;<strong>\u5e8f\u5217<\/strong>&nbsp;\u6570\u636e\u7c7b\u578b\u7684\u4e24\u4e2a\u4f8b\u5b50 (\u53c2\u8003&nbsp;<a href=\"https:\/\/docspy3zh.readthedocs.io\/en\/latest\/library\/stdtypes.html#typesseq\">Sequence Types \u2014 str, bytes, bytearray, list, tuple, range<\/a>). Python \u4f5c\u4e3a\u4e00\u95e8\u8fdb\u5316\u4e2d\u7684\u8bed\u8a00, \u53ef\u80fd\u8fd8\u6709\u5176\u5b83\u5e8f\u5217\u7c7b\u578b\u4f1a\u88ab\u52a0\u5165. \u8fd9\u91cc\u5c31\u6709\u53e6\u4e00\u79cd\u6807\u51c6\u5e8f\u5217\u6570\u636e\u7c7b\u578b:&nbsp;<strong>\u5143\u7ec4<\/strong>&nbsp;.<\/p>\n\n\n\n<p>\u5143\u7ec4\u7531\u82e5\u5e72\u9017\u53f7\u5206\u9694\u7684\u503c\u7ec4\u6210, \u4f8b\u5982:<strong>&gt;&gt;&gt; <\/strong>t = 12345, 54321, &#8216;hello!&#8217; <strong>&gt;&gt;&gt; <\/strong>t[0] 12345 <strong>&gt;&gt;&gt; <\/strong>t (12345, 54321, &#8216;hello!&#8217;) <strong>&gt;&gt;&gt; <\/strong><em># Tuples may be nested:<\/em><strong>&#8230; <\/strong>u = t, (1, 2, 3, 4, 5) <strong>&gt;&gt;&gt; <\/strong>u ((12345, 54321, &#8216;hello!&#8217;), (1, 2, 3, 4, 5))<\/p>\n\n\n\n<p>\u5982\u4f60\u6240\u89c1, \u5143\u7ec4\u8f93\u51fa\u65f6\u7528\u5706\u62ec\u53f7\u5305\u56f4, \u4ee5\u4fbf\u6b63\u786e\u8868\u8fbe\u5143\u7ec4\u7684\u5d4c\u5957; \u5728\u8f93\u5165\u65f6\u5706\u62ec\u53f7\u53ef\u52a0\u53ef\u4e0d\u52a0, \u4e0d\u8fc7\u5706\u62ec\u53f7\u7ecf\u5e38\u662f\u5fc5\u8981\u7684 (\u7279\u522b\u662f\u5f53\u5143\u7ec4\u662f\u66f4\u5927\u7684\u8868\u8fbe\u5f0f\u7684\u4e00\u90e8\u5206\u65f6).<\/p>\n\n\n\n<p>\u5143\u7ec4\u6709\u8bb8\u591a\u7528\u9014. \u4f8b\u5982: (x, y) \u5750\u6807\u5bf9, \u6570\u636e\u5e93\u91cc\u7684\u5458\u5de5\u8bb0\u5f55\u7b49. \u5143\u7ec4\u540c\u5b57\u4e32\u90fd\u662f\u4e0d\u53ef\u53d8\u7684: \u65e0\u6cd5\u5bf9\u5143\u7ec4\u6307\u5b9a\u9879\u8fdb\u884c\u8d4b\u503c (\u5c3d\u7ba1\u53ef\u901a\u8fc7\u5207\u7247\u548c\u8fde\u63a5\u6765\u6a21\u62df\u8fd9\u4e2a\u64cd\u4f5c). \u5143\u7ec4\u4e2d\u53ef\u4ee5\u5305\u542b\u53ef\u53d8\u7684\u5bf9\u8c61, \u5982\u5217\u8868.<\/p>\n\n\n\n<p>\u6784\u9020\u5305\u542b 0 \u6216 1 \u4e2a\u9879\u7684\u5143\u7ec4\u662f\u4e2a\u7279\u6b8a\u95ee\u9898: \u8bed\u6cd5\u4e0a\u4e3a\u4e86\u9002\u5e94\u8fd9\u4e00\u60c5\u51b5,\u6709\u4e9b\u989d\u5916\u7684\u89c4\u5219. \u7a7a\u5143\u7ec4\u7531\u4e00\u5bf9\u7a7a\u7684\u5706\u62ec\u53f7\u6784\u9020; \u4e00\u4e2a\u9879\u7684\u5143\u7ec4\u7531\u4e00\u4e2a\u503c\u540e\u9762\u8ddf\u7740\u4e00\u4e2a\u9017\u53f7\u6784\u9020 (\u628a\u4e00\u4e2a\u503c\u653e\u5165\u4e00\u5bf9\u5706\u62ec\u53f7\u91cc\u5e76\u4e0d\u8db3\u4ee5\u6784\u9020\u4e00\u4e2a\u5143\u7ec4). \u4e11\u964b, \u4f46\u6709\u6548. \u4f8b\u5982:<strong>&gt;&gt;&gt; <\/strong>empty = () <strong>&gt;&gt;&gt; <\/strong>singleton = &#8216;hello&#8217;, <em># &lt;&#8211; \u6ce8\u610f\u540e\u9762\u7684\u9017\u53f7<\/em><strong>&gt;&gt;&gt; <\/strong>len(empty) 0 <strong>&gt;&gt;&gt; <\/strong>len(singleton) 1 <strong>&gt;&gt;&gt; <\/strong>singleton (&#8216;hello&#8217;,)<\/p>\n\n\n\n<p>\u8bed\u53e5&nbsp;<code>t&nbsp;=&nbsp;12345,&nbsp;54321,&nbsp;'hello!'<\/code>&nbsp;\u662f&nbsp;<strong>\u5143\u7ec4\u6253\u5305<\/strong>&nbsp;\u7684\u4e00\u4e2a\u4f8b\u5b50: \u503c&nbsp;<code>12345<\/code>,&nbsp;<code>54321<\/code>&nbsp;\u548c&nbsp;<code>'hello!'<\/code>&nbsp;\u88ab\u6253\u5305\u8fdb\u4e00\u4e2a\u5143\u7ec4. \u53cd\u8fc7\u6765, \u8fd9\u4e2a\u64cd\u4f5c\u4e5f\u662f\u53ef\u884c\u7684:<strong>&gt;&gt;&gt; <\/strong>x, y, z = t<\/p>\n\n\n\n<p>\u8fd9\u79cd\u5bf9\u53f3\u4fa7\u4efb\u4e00\u5e8f\u5217\u7684\u5904\u7406\u5f88\u5408\u9002\u79f0\u4e3a&nbsp;<strong>\u5e8f\u5217\u89e3\u5305<\/strong>&nbsp;. \u5e8f\u5217\u89e3\u5305\u65f6\u8981\u6c42\u7b49\u53f7\u5de6\u8fb9\u7684\u503c\u4e2a\u6570\u4e0e\u53f3\u8fb9\u5e8f\u5217\u5143\u7d20\u4e2a\u6570\u76f8\u7b49. \u6ce8\u610f, \u591a\u91cd\u8d4b\u503c\u5176\u5b9e\u662f\u8054\u5408\u4f7f\u7528\u4e86\u5143\u7ec4\u6253\u5305\u548c\u5e8f\u5217\u89e3\u5305. (\u867d\u7136\u5143\u7ec4\u548c\u5217\u8868\u90fd\u7b97\u5e8f\u5217,\u4f46\u662f\u5fc5\u987b\u6709\u6240\u4e0d\u540c)<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">5.4. \u96c6\u5408(Set)<a href=\"https:\/\/docspy3zh.readthedocs.io\/en\/latest\/tutorial\/datastructures.html#set\"><\/a><\/h2>\n\n\n\n<p>Python \u8fd8\u5305\u542b\u4e86&nbsp;<em>\u96c6\u5408(set)<\/em>&nbsp;\u6570\u636e\u7c7b\u578b. \u96c6\u5408\u662f\u79cd\u65e0\u5e8f\u4e0d\u91cd\u590d\u7684\u5143\u7d20\u96c6. \u57fa\u672c\u7528\u9014\u5305\u62ec\u6210\u5458\u5173\u7cfb\u6d4b\u8bd5\u548c\u91cd\u590d\u6761\u76ee\u6d88\u9664. \u96c6\u5408\u5bf9\u8c61\u4e5f\u652f\u6301\u5408(union),\u4ea4(intersection), \u5dee(difference), \u548c\u5bf9\u79f0\u5dee(sysmmetric difference)\u7b49\u6570\u5b66\u64cd\u4f5c.<\/p>\n\n\n\n<p>\u82b1\u62ec\u53f7\u6216\u51fd\u5f0f&nbsp;<a href=\"https:\/\/docspy3zh.readthedocs.io\/en\/latest\/library\/stdtypes.html#set\"><code>set()<\/code><\/a>&nbsp;\u53ef\u7528\u4e8e\u521b\u5efa\u96c6\u5408. \u6ce8\u610f: \u521b\u5efa\u4e00\u4e2a\u7a7a\u96c6\u5408\u53ea\u80fd\u4f7f\u7528&nbsp;<code>set()<\/code>, \u800c\u4e0d\u80fd\u4f7f\u7528&nbsp;<code>{}<\/code>; \u540e\u8005\u662f\u521b\u5efa\u4e00\u4e2a\u7a7a\u5b57\u5178, \u5b57\u5178\u6211\u4eec\u4f1a\u5728\u4e0b\u4e00\u8282\u91cc\u8ba8\u8bba.<\/p>\n\n\n\n<p>\u4ee5\u4e0b\u662f\u7b80\u660e\u793a\u8303:<strong>&gt;&gt;&gt; <\/strong>basket = {&#8216;apple&#8217;, &#8216;orange&#8217;, &#8216;apple&#8217;, &#8216;pear&#8217;, &#8216;orange&#8217;, &#8216;banana&#8217;} <strong>&gt;&gt;&gt; <\/strong>print(basket) <em># \u91cd\u590d\u7684\u88ab\u79fb\u9664\u4e86<\/em> {&#8216;orange&#8217;, &#8216;banana&#8217;, &#8216;pear&#8217;, &#8216;apple&#8217;} <strong>&gt;&gt;&gt; <\/strong>&#8216;orange&#8217; <strong>in<\/strong> basket <em># \u5feb\u901f\u6210\u5458\u5173\u7cfb\u6d4b\u8bd5<\/em> True <strong>&gt;&gt;&gt; <\/strong>&#8216;crabgrass&#8217; <strong>in<\/strong> basket False <strong>&gt;&gt;&gt; <\/strong><em># \u5728\u4e24\u4e2a\u5355\u8bcd\u7684\u4e0d\u91cd\u590d\u7684\u5b57\u6bcd\u91cc\u6f14\u793a\u96c6\u5408\u64cd\u4f5c<\/em><strong>&#8230;<\/strong><strong>&gt;&gt;&gt; <\/strong>a = set(&#8216;abracadabra&#8217;) <strong>&gt;&gt;&gt; <\/strong>b = set(&#8216;alacazam&#8217;) <strong>&gt;&gt;&gt; <\/strong>a <em># a \u4e2d\u7684\u4e0d\u91cd\u590d\u5b57\u6bcd<\/em> {&#8216;a&#8217;, &#8216;r&#8217;, &#8216;b&#8217;, &#8216;c&#8217;, &#8216;d&#8217;} <strong>&gt;&gt;&gt; <\/strong>a &#8211; b <em># a \u4e2d\u6709\u800c b \u4e2d\u6ca1\u6709\u7684\u5b57\u6bcd<\/em> {&#8216;r&#8217;, &#8216;d&#8217;, &#8216;b&#8217;} <strong>&gt;&gt;&gt; <\/strong>a | b <em># \u65e2\u6709 a \u7684\u5b57\u6bcd\u53c8\u6709 b \u7684\u5b57\u6bcd<\/em> {&#8216;a&#8217;, &#8216;c&#8217;, &#8216;r&#8217;, &#8216;d&#8217;, &#8216;b&#8217;, &#8216;m&#8217;, &#8216;z&#8217;, &#8216;l&#8217;} <strong>&gt;&gt;&gt; <\/strong>a &amp; b <em># a \u548c b \u4e2d\u90fd\u6709\u7684\u5b57\u6bcd<\/em> {&#8216;a&#8217;, &#8216;c&#8217;} <strong>&gt;&gt;&gt; <\/strong>a ^ b <em># a \u6216 b \u4e2d\u53ea\u6709\u4e00\u4e2a\u6709\u7684\u5b57\u6bcd<\/em> {&#8216;r&#8217;, &#8216;d&#8217;, &#8216;b&#8217;, &#8216;m&#8217;, &#8216;z&#8217;, &#8216;l&#8217;}<\/p>\n\n\n\n<p>\u5c31\u50cf&nbsp;<a href=\"https:\/\/docspy3zh.readthedocs.io\/en\/latest\/tutorial\/datastructures.html#tut-listcomps\">\u5217\u8868<\/a>, \u96c6\u5408\u4e5f\u652f\u6301\u63a8\u5bfc\u5f0f:<strong>&gt;&gt;&gt; <\/strong>a = {x <strong>for<\/strong> x <strong>in<\/strong> &#8216;abracadabra&#8217; <strong>if<\/strong> x <strong>not<\/strong><strong>in<\/strong> &#8216;abc&#8217;} <strong>&gt;&gt;&gt; <\/strong>a {&#8216;r&#8217;, &#8216;d&#8217;}<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">5.5. \u5b57\u5178<a href=\"https:\/\/docspy3zh.readthedocs.io\/en\/latest\/tutorial\/datastructures.html#tut-dictionaries\"><\/a><\/h2>\n\n\n\n<p>Python \u4e2d\u53e6\u4e00\u5f88\u6709\u7528\u7684\u5185\u5efa\u6570\u636e\u7c7b\u578b\u4e3a&nbsp;<em>\u5b57\u5178<\/em>&nbsp;(\u53c2\u8003&nbsp;<a href=\"https:\/\/docspy3zh.readthedocs.io\/en\/latest\/library\/stdtypes.html#typesmapping\">Mapping Types \u2014 dict<\/a>). \u5728\u5176\u5b83\u8bed\u8a00\u4e2d\u5b57\u5178\u4e00\u822c\u88ab\u53eb\u505a \u201c\u5173\u8054\u5b58\u50a8\u201d \u6216 \u201c\u5173\u8054\u6570\u7ec4\u201d. \u4e0e\u4f7f\u7528\u67d0\u4e2a\u8303\u56f4\u4f5c\u4e3a\u7d22\u5f15\u7684\u5e8f\u5217\u4e0d\u4e00\u6837, \u5b57\u5178\u901a\u8fc7&nbsp;<em>\u952e<\/em>&nbsp;\u6765\u7d22\u5f15, \u800c\u952e\u53ef\u4ee5\u662f\u4efb\u610f\u4e0d\u53ef\u53d8\u7c7b\u578b; \u901a\u5e38\u7528\u5b57\u7b26\u4e32\u548c\u6570\u5b57\u4f5c\u4e3a\u952e. \u5982\u679c\u5143\u7ec4\u53ea\u5305\u542b\u5b57\u7b26\u4e32\u548c\u6570\u5b57, \u5143\u7ec4\u4e5f\u53ef\u4ee5\u4f5c\u4e3a\u952e; \u4f46\u662f, \u5f53\u5143\u7ec4\u76f4\u63a5\u6216\u95f4\u63a5\u5730\u5305\u542b\u53ef\u53d8\u5bf9\u8c61\u65f6, \u5c31\u4e0d\u80fd\u7528\u4f5c\u4e00\u4e2a\u952e. \u4e0d\u80fd\u4f7f\u7528\u5217\u8868\u4f5c\u4e3a\u952e, \u56e0\u4e3a\u5217\u8868\u53ef\u4ee5\u901a\u8fc7\u7d22\u5f15, \u5207\u7247, \u6216\u5982&nbsp;<code>append()<\/code>&nbsp;\u548c&nbsp;<code>extend()<\/code>&nbsp;\u65b9\u6cd5\u539f\u5730\u8d4b\u503c\u800c\u88ab\u6539\u53d8.<\/p>\n\n\n\n<p>\u6700\u597d\u628a\u5b57\u5178\u770b\u6210\u662f\u4e00\u4e2a\u6ca1\u6709\u987a\u5e8f\u7684&nbsp;<em>\u952e:\u503c<\/em>&nbsp;\u5bf9\u96c6\u5408, \u952e\u5fc5\u987b\u662f\u552f\u4e00\u7684 (\u5728\u4e00\u4e2a\u5b57\u5178\u91cc). \u4e00\u5bf9\u82b1\u62ec\u53f7\u521b\u5efa\u4e00\u4e2a\u7a7a\u5b57\u5178:&nbsp;<code>{}<\/code>. \u5728\u62ec\u53f7\u4e2d\u95f4\u653e\u7f6e\u7684\u4ee5\u9017\u53f7\u5206\u9694\u7684&nbsp;<code>\u952e:\u503c\u5bf9<\/code>&nbsp;\u5217\u8868\u5c31\u662f\u5b57\u5178\u7684\u521d\u59cb&nbsp;<code>\u952e:\u503c\u5bf9<\/code>. \u8fd9\u4e5f\u662f\u5b57\u5178\u8f93\u51fa\u65f6\u7684\u683c\u5f0f.<\/p>\n\n\n\n<p>\u5b57\u5178\u6700\u4e3b\u8981\u7684\u64cd\u4f5c\u662f\u901a\u8fc7\u67d0\u952e\u5b58\u50a8\u4e00\u4e2a\u503c, \u4ee5\u53ca\u4ece\u7ed9\u5b9a\u7684\u952e\u91cc\u63d0\u53d6\u5b83\u7684\u503c. \u4f7f\u7528&nbsp;<code>del<\/code>&nbsp;\u53ef\u4ee5\u5220\u9664\u4e00\u4e2a\u952e:\u503c\u5bf9. \u5982\u679c\u4f60\u4f7f\u7528\u4e00\u4e2a\u5df2\u88ab\u4f7f\u7528\u7684\u952e\u8fdb\u884c\u5b58\u50a8\u64cd\u4f5c, \u8be5\u952e\u7684\u65e7\u503c\u5c31\u6ca1\u6709\u4e86. \u4f7f\u7528\u4e00\u4e2a\u4e0d\u5b58\u5728\u7684\u952e\u63d0\u53d6\u503c\u4f1a\u4ea7\u751f\u4e00\u4e2a\u9519\u8bef.<\/p>\n\n\n\n<p>\u5728\u4e00\u4e2a\u5b57\u5178\u4e0a\u6267\u884c&nbsp;<code>list(d.keys())<\/code>&nbsp;\u8fd4\u56de\u8be5\u5b57\u5178\u4e2d\u6240\u4f7f\u7528\u952e\u7684\u5217\u8868, \u8be5\u5217\u8868\u7684\u987a\u5e8f\u4e0d\u786e\u5b9a (\u5982\u679c\u9700\u8981\u6709\u5e8f, \u53ea\u8981\u4f7f\u7528&nbsp;<code>sorted(d.keys())<\/code>).&nbsp;<a href=\"https:\/\/docspy3zh.readthedocs.io\/en\/latest\/tutorial\/datastructures.html#id19\">[1]<\/a>&nbsp;\u8981\u68c0\u67e5\u67d0\u4e00\u4e2a\u952e\u662f\u5426\u5728\u5b57\u5178\u91cc, \u4f7f\u7528&nbsp;<a href=\"https:\/\/docspy3zh.readthedocs.io\/en\/latest\/reference\/expressions.html#in\"><code>in<\/code><\/a>&nbsp;\u5173\u952e\u5b57.<\/p>\n\n\n\n<p>\u8fd9\u662f\u4e00\u4e2a\u4f7f\u7528\u5b57\u5178\u7684\u5c0f\u4f8b\u5b50:<strong>&gt;&gt;&gt; <\/strong>tel = {&#8216;jack&#8217;: 4098, &#8216;sape&#8217;: 4139} <strong>&gt;&gt;&gt; <\/strong>tel[&#8216;guido&#8217;] = 4127 <strong>&gt;&gt;&gt; <\/strong>tel {&#8216;sape&#8217;: 4139, &#8216;guido&#8217;: 4127, &#8216;jack&#8217;: 4098} <strong>&gt;&gt;&gt; <\/strong>tel[&#8216;jack&#8217;] 4098 <strong>&gt;&gt;&gt; <\/strong><strong>del<\/strong> tel[&#8216;sape&#8217;] <strong>&gt;&gt;&gt; <\/strong>tel[&#8216;irv&#8217;] = 4127 <strong>&gt;&gt;&gt; <\/strong>tel {&#8216;guido&#8217;: 4127, &#8216;irv&#8217;: 4127, &#8216;jack&#8217;: 4098} <strong>&gt;&gt;&gt; <\/strong>list(tel.keys()) [&#8216;irv&#8217;, &#8216;guido&#8217;, &#8216;jack&#8217;] <strong>&gt;&gt;&gt; <\/strong>sorted(tel.keys()) [&#8216;guido&#8217;, &#8216;irv&#8217;, &#8216;jack&#8217;] <strong>&gt;&gt;&gt; <\/strong>&#8216;guido&#8217; <strong>in<\/strong> tel True <strong>&gt;&gt;&gt; <\/strong>&#8216;jack&#8217; <strong>not<\/strong><strong>in<\/strong> tel False<\/p>\n\n\n\n<p>\u6784\u9020\u5668&nbsp;<a href=\"https:\/\/docspy3zh.readthedocs.io\/en\/latest\/library\/stdtypes.html#dict\"><code>dict()<\/code><\/a>&nbsp;\u4ece\u952e-\u503c\u5bf9\u5e8f\u5217\u91cc\u76f4\u63a5\u751f\u6210\u5b57\u5178,\u5982\u679c\u6709\u56fa\u5b9a\u7684\u6a21\u5f0f,\u53ef\u5728\u5217\u8868\u63a8\u5bfc\u5f0f\u6307\u5b9a\u7279\u5b9a\u7684\u952e\u503c\u5bf9:<strong>&gt;&gt;&gt; <\/strong>dict([(&#8216;sape&#8217;, 4139), (&#8216;guido&#8217;, 4127), (&#8216;jack&#8217;, 4098)]) {&#8216;sape&#8217;: 4139, &#8216;jack&#8217;: 4098, &#8216;guido&#8217;: 4127} <strong>&gt;&gt;&gt; <\/strong>dict([(x, x**2) <strong>for<\/strong> x <strong>in<\/strong> (2, 4, 6)]) <em># \u4f7f\u7528\u5217\u8868\u63a8\u5bfc\u5f0f<\/em> {2: 4, 4: 16, 6: 36}<\/p>\n\n\n\n<p>\u5728\u672c\u6559\u7a0b\u540e\u9762\u7684\u7ae0\u8282, \u4f1a\u5b66\u4e60\u5230\u751f\u6210\u5668\u8868\u8fbe\u5f0f, \u8fd9\u66f4\u9002\u4e8e\u4e3a&nbsp;<a href=\"https:\/\/docspy3zh.readthedocs.io\/en\/latest\/library\/stdtypes.html#dict\"><code>dict()<\/code><\/a>&nbsp;\u6784\u9020\u5668\u751f\u6210\u952e-\u503c\u5bf9\u5e8f\u5217. \u82e5\u952e\u4e3a\u5b57\u7b26\u4e32, \u6709\u65f6\u7528\u5173\u952e\u5b57\u53c2\u6570\u6307\u5b9a\u952e-\u503c\u5bf9\u66f4\u4e3a\u7b80\u5355:<strong>&gt;&gt;&gt; <\/strong>dict(sape=4139, guido=4127, jack=4098) {&#8216;sape&#8217;: 4139, &#8216;jack&#8217;: 4098, &#8216;guido&#8217;: 4127}<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">5.6. \u904d\u5386\u6280\u5de7<a href=\"https:\/\/docspy3zh.readthedocs.io\/en\/latest\/tutorial\/datastructures.html#tut-loopidioms\"><\/a><\/h2>\n\n\n\n<p>\u5f53\u5bf9\u5b57\u5178\u904d\u5386\u65f6, \u53ef\u7528&nbsp;<code>items()<\/code>&nbsp;\u65b9\u6cd5\u540c\u65f6\u53d6\u56de\u952e\u548c\u5bf9\u5e94\u7684\u503c.<strong>&gt;&gt;&gt; <\/strong>knights = {&#8216;gallahad&#8217;: &#8216;the pure&#8217;, &#8216;robin&#8217;: &#8216;the brave&#8217;} <strong>&gt;&gt;&gt; <\/strong><strong>for<\/strong> k, v <strong>in<\/strong> knights.items(): <strong>&#8230;<\/strong> print(k, v) <strong>&#8230;<\/strong> gallahad the pure robin the brave<\/p>\n\n\n\n<p>\u5bf9\u5e8f\u5217\u904d\u5386\u65f6, \u53ef\u4ee5\u4f7f\u7528&nbsp;<a href=\"https:\/\/docspy3zh.readthedocs.io\/en\/latest\/library\/functions.html#enumerate\"><code>enumerate()<\/code><\/a>&nbsp;\u51fd\u5f0f\u6765\u540c\u65f6\u53d6\u56de\u4f4d\u7f6e\u7d22\u5f15\u548c\u76f8\u5e94\u7684\u503c.<strong>&gt;&gt;&gt; <\/strong><strong>for<\/strong> i, v <strong>in<\/strong> enumerate([&#8216;tic&#8217;, &#8216;tac&#8217;, &#8216;toe&#8217;]): <strong>&#8230;<\/strong> print(i, v) <strong>&#8230;<\/strong> 0 tic 1 tac 2 toe<\/p>\n\n\n\n<p>\u540c\u65f6\u5bf9\u4e24\u4e2a\u6216\u66f4\u591a\u7684\u5e8f\u5217\u8fdb\u884c\u904d\u5386\u65f6, \u53ef\u7528&nbsp;<a href=\"https:\/\/docspy3zh.readthedocs.io\/en\/latest\/library\/functions.html#zip\"><code>zip()<\/code><\/a>&nbsp;\u8fdb\u884c\u7ec4\u5408<strong>&gt;&gt;&gt; <\/strong>questions = [&#8216;name&#8217;, &#8216;quest&#8217;, &#8216;favorite color&#8217;] <strong>&gt;&gt;&gt; <\/strong>answers = [&#8216;lancelot&#8217;, &#8216;the holy grail&#8217;, &#8216;blue&#8217;] <strong>&gt;&gt;&gt; <\/strong><strong>for<\/strong> q, a <strong>in<\/strong> zip(questions, answers): <strong>&#8230;<\/strong> print(&#8216;What is your <em>{0}<\/em>? It is <em>{1}<\/em>.&#8217;.format(q, a)) <strong>&#8230;<\/strong> What is your name? It is lancelot. What is your quest? It is the holy grail. What is your favorite color? It is blue.<\/p>\n\n\n\n<p>\u53cd\u5411\u904d\u5386\u5e8f\u5217\u65f6, \u5148\u6307\u5b9a\u8fd9\u4e2a\u5e8f\u5217, \u7136\u540e\u8c03\u7528&nbsp;<a href=\"https:\/\/docspy3zh.readthedocs.io\/en\/latest\/library\/functions.html#reversed\"><code>reversed()<\/code><\/a>&nbsp;\u51fd\u5f0f<strong>&gt;&gt;&gt; <\/strong><strong>for<\/strong> i <strong>in<\/strong> reversed(range(1, 10, 2)): <strong>&#8230;<\/strong> print(i) <strong>&#8230;<\/strong> 9 7 5 3 1<\/p>\n\n\n\n<p>\u60f3\u6709\u5e8f\u5730\u904d\u5386\u4e00\u4e2a\u5e8f\u5217, \u7528&nbsp;<a href=\"https:\/\/docspy3zh.readthedocs.io\/en\/latest\/library\/functions.html#sorted\"><code>sorted()<\/code><\/a>&nbsp;\u51fd\u5f0f\u8fd4\u56de\u6392\u5e8f\u540e\u7684\u5e8f\u5217,\u539f\u5e8f\u5217\u5c06\u4e0d\u88ab\u89e6\u53ca<strong>&gt;&gt;&gt; <\/strong>basket = [&#8216;apple&#8217;, &#8216;orange&#8217;, &#8216;apple&#8217;, &#8216;pear&#8217;, &#8216;orange&#8217;, &#8216;banana&#8217;] <strong>&gt;&gt;&gt; <\/strong><strong>for<\/strong> f <strong>in<\/strong> sorted(set(basket)): <strong>&#8230;<\/strong> print(f) <strong>&#8230;<\/strong> apple banana orange pear<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">5.7. \u6df1\u5165\u6761\u4ef6\u63a7\u5236<a href=\"https:\/\/docspy3zh.readthedocs.io\/en\/latest\/tutorial\/datastructures.html#tut-conditions\"><\/a><\/h2>\n\n\n\n<p>\u5728&nbsp;<code>while<\/code>&nbsp;\u548c&nbsp;<code>if<\/code>&nbsp;\u8bed\u53e5\u4e2d\u4f7f\u7528\u7684\u6761\u4ef6\u53ef\u4ee5\u5305\u542b\u4efb\u4f55\u64cd\u4f5c\u7b26, \u800c\u4e0d\u4ec5\u4ec5\u662f\u6bd4\u8f83.<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u6bd4\u8f83\u64cd\u4f5c\u7b26&nbsp;<code>in<\/code>&nbsp;\u548c&nbsp;<code>not&nbsp;in<\/code>&nbsp;\u68c0\u67e5\u4e00\u4e2a\u503c\u662f\u5426\u5728\u5e8f\u5217\u4e2d.<\/li>\n\n\n\n<li>\u64cd\u4f5c\u7b26&nbsp;<code>is<\/code>&nbsp;\u548c&nbsp;<code>is&nbsp;not<\/code>&nbsp;\u6bd4\u8f83\u4e24\u4e2a\u5bf9\u8c61\u662f\u5426\u4e3a\u540c\u4e00\u5bf9\u8c61; \u8fd9\u53ea\u5bf9\u8bf8\u5982\u5217\u8868\u7684\u53ef\u53d8\u5bf9\u8c61\u6709\u7528.<\/li>\n<\/ul>\n\n\n\n<p>\u6240\u6709\u6bd4\u8f83\u64cd\u4f5c\u7b26\u5177\u6709\u76f8\u540c\u7684\u4f18\u5148\u7ea7, \u4f4e\u4e8e\u6240\u6709\u7684\u6570\u503c\u64cd\u4f5c.<\/p>\n\n\n\n<p>\u6bd4\u8f83\u64cd\u4f5c\u7b26\u53ef\u4ee5\u8fde\u8d77\u6765\u4f7f\u7528. \u4f8b\u5982, &#8211;&nbsp;<code>a&nbsp;&lt;&nbsp;b&nbsp;==&nbsp;c<\/code>&nbsp;\u6d4b\u8bd5&nbsp;<code>a<\/code>&nbsp;\u5c0f\u4e8e&nbsp;<code>b<\/code>&nbsp;\u4e14&nbsp;<code>b<\/code>&nbsp;\u4e0e&nbsp;<code>c<\/code>&nbsp;\u76f8\u7b49.<\/p>\n\n\n\n<p>\u6bd4\u8f83\u64cd\u4f5c(\u6216\u5176\u5b83\u4efb\u4f55\u5e03\u5c14\u8868\u8fbe\u5f0f)\u90fd\u80fd\u7528\u903b\u8f91\u64cd\u4f5c\u7b26&nbsp;<code>and<\/code>&nbsp;\u548c&nbsp;<code>or<\/code>&nbsp;\u8fde\u63a5, \u7ed3\u679c\u503c\u53ef\u4ee5\u7528&nbsp;<code>not<\/code>&nbsp;\u53d6\u53cd.<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u903b\u8f91\u64cd\u4f5c\u7b26\u7684\u4f18\u5148\u7ea7\u53c8\u4f4e\u4e8e\u6bd4\u8f83\u64cd\u4f5c\u7b26;<\/li>\n\n\n\n<li>\u8fd9\u5176\u4e2d,&nbsp;<code>not<\/code>&nbsp;\u4f18\u5148\u7ea7\u6700\u9ad8, \u800c&nbsp;<code>or<\/code>&nbsp;\u7684\u4f18\u5148\u7ea7\u6700\u4f4e, \u56e0\u6b64&nbsp;<code>A&nbsp;and&nbsp;not&nbsp;B&nbsp;or&nbsp;C<\/code>&nbsp;\u7b49\u4ef7\u4e8e&nbsp;<code>(A&nbsp;and&nbsp;(not&nbsp;B))&nbsp;or&nbsp;C<\/code>&nbsp;. \u540c\u6837, \u53ef\u4ee5\u4f7f\u7528\u5706\u62ec\u53f7\u6765\u8868\u8fbe\u60f3\u8981\u7684\u7ed3\u679c.<\/li>\n\n\n\n<li>\u903b\u8f91\u64cd\u4f5c\u7b26&nbsp;<code>and<\/code>&nbsp;\u548c&nbsp;<code>or<\/code>&nbsp;\u88ab\u79f0\u4e3a&nbsp;<em>\u77ed\u8def<\/em>&nbsp;\u64cd\u4f5c\u7b26: \u5b83\u4ece\u5de6\u81f3\u53f3\u8ba1\u7b97\u53c2\u6570,\u5e76\u4e14\u5f53\u7ed3\u679c\u786e\u5b9a\u65f6\u8ba1\u7b97\u5c31\u7acb\u5373\u505c\u6b62.\n<ul class=\"wp-block-list\">\n<li>\u4f8b\u5982, \u5982\u679c&nbsp;<code>A<\/code>&nbsp;\u548c&nbsp;<code>C<\/code>&nbsp;\u4e3a\u771f, \u800c&nbsp;<code>B<\/code>&nbsp;\u4e3a\u5047\u65f6,&nbsp;<code>A&nbsp;and&nbsp;B&nbsp;and&nbsp;C<\/code>&nbsp;\u4e0d\u4f1a\u8ba1\u7b97\u8868\u8fbe\u5f0f&nbsp;<code>C<\/code>.<\/li>\n\n\n\n<li>\u5f53\u628a\u77ed\u8def\u64cd\u4f5c\u7b26\u7684\u8fd4\u56de\u503c\u4f5c\u4e3a\u4e00\u4e2a\u5e38\u89c4\u503c\u800c\u4e0d\u662f\u5e03\u5c14\u503c\u65f6, \u5b83\u7684\u503c\u5c31\u662f\u6700\u540e\u8ba1\u7b97\u7684\u53c2\u6570\u503c.<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n\n\n\n<p>\u53ef\u4ee5\u628a\u6bd4\u8f83\u5f0f\u6216\u5176\u5b83\u903b\u8f91\u8868\u8fbe\u5f0f\u7684\u503c\u8d4b\u7ed9\u4e00\u4e2a\u53d8\u91cf. \u4f8b\u5982,<strong>&gt;&gt;&gt; <\/strong>string1, string2, string3 = &#8221;, &#8216;Trondheim&#8217;, &#8216;Hammer Dance&#8217; <strong>&gt;&gt;&gt; <\/strong>non_null = string1 <strong>or<\/strong> string2 <strong>or<\/strong> string3 <strong>&gt;&gt;&gt; <\/strong>non_null &#8216;Trondheim&#8217;<\/p>\n\n\n\n<p>\u6ce8\u610f, \u5728 Python \u4e2d, \u4e0d\u50cf C, \u8d4b\u503c\u4e0d\u53ef\u4ee5\u53d1\u751f\u5728\u8868\u8fbe\u5f0f\u5185\u90e8. C \u7a0b\u5e8f\u5458\u53ef\u80fd\u5bf9\u6b64\u6709\u62b1\u6028, \u4f46\u662f\u8fd9\u6837\u5c31\u907f\u514d\u4e86 C \u7a0b\u5e8f\u4e2d\u5e38\u89c1\u7684\u4e00\u7c7b\u9519\u8bef,\u6bd4\u5982\u8bf4:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u5728\u4f7f\u7528&nbsp;<code>==<\/code>&nbsp;\u7684\u8868\u8fbe\u5f0f\u91cc\u952e\u5165\u4e86&nbsp;<code>=<\/code>&nbsp;.<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">5.8. \u5e8f\u5217\u548c\u5176\u5b83\u7c7b\u578b\u7684\u6bd4\u8f83<a href=\"https:\/\/docspy3zh.readthedocs.io\/en\/latest\/tutorial\/datastructures.html#tut-comparing\"><\/a><\/h2>\n\n\n\n<p>\u5e8f\u5217\u5bf9\u8c61\u53ef\u4ee5\u4e0e\u540c\u4e00\u7c7b\u578b\u7684\u5176\u5b83\u5bf9\u8c61\u6bd4\u8f83. \u4f7f\u7528&nbsp;<em>\u5b57\u5178\u7f16\u7e82<\/em>&nbsp;\u987a\u5e8f\u6bd4\u8f83:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u9996\u5148\u6bd4\u8f83\u5934\u4e24\u9879, \u5982\u679c\u5b83\u4eec\u4e0d\u540c, \u5b83\u4eec\u7684\u6bd4\u8f83\u5c31\u51b3\u5b9a\u6574\u4e2a\u6bd4\u8f83\u7684\u7ed3\u679c;<\/li>\n\n\n\n<li>\u5982\u679c\u5b83\u4eec\u76f8\u540c, \u5c31\u6bd4\u8f83\u4e0b\u4e24\u9879, \u5c31\u8fd9\u6837\u76f4\u5230\u5176\u4e2d\u6709\u5e8f\u5217\u88ab\u6bd4\u8f83\u5b8c\u4e86.<\/li>\n\n\n\n<li>\u5982\u679c\u8981\u88ab\u6bd4\u8f83\u7684\u4e24\u9879\u672c\u8eab\u5c31\u662f\u76f8\u540c\u7c7b\u578b\u7684\u5e8f\u5217, \u90a3\u4e48\u5c31\u9012\u5f52\u8fdb\u884c\u6bd4\u8f83.<\/li>\n\n\n\n<li>\u5982\u679c\u4e24\u4e2a\u5e8f\u5217\u6240\u6709\u7684\u9879\u90fd\u76f8\u7b49, \u90a3\u4e48, \u5b83\u4eec\u5c31\u76f8\u7b49.<\/li>\n\n\n\n<li>\u5982\u679c\u4e00\u4e2a\u5e8f\u5217\u662f\u53e6\u4e00\u4e2a\u5e8f\u5217\u7684\u521d\u59cb\u5b50\u5e8f\u5217(initial sub-sequence),\u90a3\u4e48\u77ed\u7684\u5c31\u662f\u8f83\u5c0f\u7684.<\/li>\n\n\n\n<li>\u5b57\u7b26\u4e32\u7684&nbsp;<em>\u5b57\u5178\u7f16\u7e82<\/em>&nbsp;\u987a\u5e8f\u7531\u5355\u4e2a\u5b57\u7b26\u7684 Unicode \u5b57\u7801\u6765\u51b3\u5b9a.<\/li>\n<\/ul>\n\n\n\n<p>\u4ee5\u4e0b\u662f\u6bd4\u8f83\u76f8\u540c\u7c7b\u578b\u5e8f\u5217\u7684\u4f8b\u5b50:(1, 2, 3) &lt; (1, 2, 4) [1, 2, 3] &lt; [1, 2, 4] &#8216;ABC&#8217; &lt; &#8216;C&#8217; &lt; &#8216;Pascal&#8217; &lt; &#8216;Python&#8217; (1, 2, 3, 4) &lt; (1, 2, 4) (1, 2) &lt; (1, 2, -1) (1, 2, 3) == (1.0, 2.0, 3.0) (1, 2, (&#8216;aa&#8217;, &#8216;ab&#8217;)) &lt; (1, 2, (&#8216;abc&#8217;, &#8216;a&#8217;), 4)<\/p>\n\n\n\n<p>\u6ce8\u610f, \u4f7f\u7528&nbsp;<code>&lt;<\/code>&nbsp;\u6216&nbsp;<code>&gt;<\/code>&nbsp;\u6bd4\u8f83\u4e24\u4e2a\u4e0d\u540c\u7c7b\u578b\u7684\u5bf9\u8c61\u6709\u65f6\u5019\u662f\u5408\u6cd5\u7684, \u6761\u4ef6\u662f\u5b83\u4eec\u8981\u6709\u5408\u9002\u7684\u6bd4\u8f83\u65b9\u6cd5. \u4f8b\u5982, \u4e0d\u540c\u7684\u6570\u5b57\u7c7b\u578b\u53ef\u4ee5\u6309\u7167\u5b83\u4eec\u7684\u6570\u5b57\u5927\u5c0f\u6765\u6bd4\u8f83, \u56e0\u6b64 0 \u7b49\u4e8e 0.0, \u7b49\u7b49. \u5426\u5219, \u89e3\u91ca\u5668\u4e0d\u4f1a\u63d0\u4f9b\u4e00\u4e2a\u4efb\u610f\u7684\u987a\u5e8f, \u800c\u4f1a\u629b\u51fa\u4e00\u4e2a&nbsp;<a href=\"https:\/\/docspy3zh.readthedocs.io\/en\/latest\/library\/exceptions.html#TypeError\"><code>TypeError<\/code><\/a>&nbsp;\u5f02\u5e38.<\/p>\n\n\n\n<p>Footnotes<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table><tbody><tr><td><a href=\"https:\/\/docspy3zh.readthedocs.io\/en\/latest\/tutorial\/datastructures.html#id15\">[1]<\/a><\/td><td>\u8c03\u7528&nbsp;<code>d.keys()<\/code>&nbsp;\u5c06\u8fd4\u56de\u4e00\u4e2a&nbsp;<em>dictionary view<\/em>&nbsp;\u5bf9\u8c61. \u5b83\u652f\u6301\u7c7b\u4f3c\u6210\u5458\u5173\u7cfb\u6d4b\u8bd5\u4ee5\u53ca\u8fed\u4ee3\u64cd\u4f5c, \u4f46\u662f\u5b83\u7684\u5185\u5bb9\u4e0d\u662f\u72ec\u7acb\u4e8e\u539f\u59cb\u5b57\u5178\u7684 \u2013 \u5b83\u53ea\u662f\u4e00\u4e2a*\u89c6\u56fe*.<\/td><\/tr><\/tbody><\/table><\/figure>\n","protected":false},"excerpt":{"rendered":"<p># \u5269\u4f59\u7684\u961f\u5217, \u5b83\u6309\u7167\u8fdb\u5165\u7684\u987a\u5e8f\u6392\u5217 deque([&#8216;Michael&#8217;, &#038;#821 [&hellip;]<\/p>\n","protected":false},"author":5,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[24],"tags":[],"class_list":["post-196","post","type-post","status-publish","format-standard","hentry","category-python"],"blocksy_meta":"","_links":{"self":[{"href":"http:\/\/ai.gitpp.com\/index.php\/wp-json\/wp\/v2\/posts\/196","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/ai.gitpp.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/ai.gitpp.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/ai.gitpp.com\/index.php\/wp-json\/wp\/v2\/users\/5"}],"replies":[{"embeddable":true,"href":"http:\/\/ai.gitpp.com\/index.php\/wp-json\/wp\/v2\/comments?post=196"}],"version-history":[{"count":3,"href":"http:\/\/ai.gitpp.com\/index.php\/wp-json\/wp\/v2\/posts\/196\/revisions"}],"predecessor-version":[{"id":250,"href":"http:\/\/ai.gitpp.com\/index.php\/wp-json\/wp\/v2\/posts\/196\/revisions\/250"}],"wp:attachment":[{"href":"http:\/\/ai.gitpp.com\/index.php\/wp-json\/wp\/v2\/media?parent=196"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/ai.gitpp.com\/index.php\/wp-json\/wp\/v2\/categories?post=196"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/ai.gitpp.com\/index.php\/wp-json\/wp\/v2\/tags?post=196"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}