{"id":205403,"date":"2025-05-29T09:43:23","date_gmt":"2025-05-29T01:43:23","guid":{"rendered":"https:\/\/server.hk\/cnblog\/205403\/"},"modified":"2025-05-29T09:43:23","modified_gmt":"2025-05-29T01:43:23","slug":"python-%e5%ad%97%e7%ac%a6%e4%b8%b2%e5%b1%82%e7%ba%a7%e8%a7%a3%e6%9e%90%ef%bc%9a%e5%a6%82%e4%bd%95%e5%88%a4%e6%96%ad%e4%b8%8d%e5%90%8c%e5%b1%82%e7%ba%a7%e7%9a%84%e7%ab%96%e7%ba%bf%e5%88%86%e9%9a%94","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/205403\/","title":{"rendered":"Python \u5b57\u7b26\u4e32\u5c42\u7ea7\u89e3\u6790\uff1a\u5982\u4f55\u5224\u65ad\u4e0d\u540c\u5c42\u7ea7\u7684\u7ad6\u7ebf\u5206\u9694\u7b26\uff1f"},"content":{"rendered":"<p><b><\/b>     <\/p>\n<h1>Python \u5b57\u7b26\u4e32\u5c42\u7ea7\u89e3\u6790\uff1a\u5982\u4f55\u5224\u65ad\u4e0d\u540c\u5c42\u7ea7\u7684\u7ad6\u7ebf\u5206\u9694\u7b26\uff1f<\/h1>\n<p>\u4eca\u5929\u7ed9\u5927\u5bb6\u5e26\u6765\u4e86\uff0c\u5176\u4e2d\u6d89\u53ca\u5230\u7684\u77e5\u8bc6\u70b9\u5305\u62ec<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\"><\/span>\u7b49\u7b49\uff0c\u65e0\u8bba\u4f60\u662f\u5c0f\u767d\u8fd8\u662f\u8001\u624b\uff0c\u90fd\u9002\u5408\u770b\u4e00\u770b\u54e6~\u6709\u597d\u7684\u5efa\u8bae\u4e5f\u6b22\u8fce\u5927\u5bb6\u5728\u8bc4\u8bba\u7559\u8a00\uff0c\u82e5\u662f\u770b\u5b8c\u6709\u6240\u6536\u83b7\uff0c\u4e5f\u5e0c\u671b\u5927\u5bb6\u80fd\u591a\u591a\u70b9\u8d5e\u652f\u6301\u5440\uff01\u4e00\u8d77\u52a0\u6cb9\u5b66\u4e60~<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20241201\/1733011243674ba72b03ff5.jpg\" class=\"aligncenter\"><\/p>\n<p><strong>python \u5b57\u7b26\u4e32\u5c42\u7ea7\u89e3\u6790<\/strong><\/p>\n<p>\u7ed9\u5b9a\u5b57\u7b26\u4e32\u4e2d\u5305\u542b\u591a\u4e2a\u7ad6\u7ebf\u5206\u9694\u7b26\uff0c\u5982\u4f55\u5224\u65ad\u4e0d\u540c\u5c42\u7ea7\u7684\u7ad6\u7ebf\uff1f<\/p>\n<p><strong>\u95ee\u9898\u8be6\u89e3<\/strong><\/p>\n<p>\u5728\u63d0\u4f9b\u7684\u5b57\u7b26\u4e32\u4e2d\uff0c\u5b58\u5728\u4e24\u4e2a\u7ad6\u7ebf\u5206\u9694\u7b26 &#8216;|&#8217;\uff0c\u5176\u4e2d\u5de6\u8fb9\u7684\u7ad6\u7ebf\u5206\u9694 c \u548c d\uff0c\u800c\u53f3\u8fb9\u7684\u7ad6\u7ebf\u5206\u9694 b{c|d} \u548c e\u3002\u663e\u7136\uff0c\u8fd9\u4e24\u4e2a\u7ad6\u7ebf\u5177\u6709\u4e0d\u540c\u7684\u5c42\u7ea7\u3002\u6211\u4eec\u9700\u8981\u4e00\u79cd\u65b9\u6cd5\u6765\u533a\u5206\u5b83\u4eec\u7684\u5c42\u7ea7\u3002<\/p>\n<p><strong>\u89e3\u51b3\u65b9\u6848<\/strong><\/p>\n<p>\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528 python \u5b57\u5178\u6765\u8ddf\u8e2a\u5b57\u7b26\u4e32\u4e2d\u6bcf\u4e2a\u4f4d\u7f6e\u5c5e\u4e8e\u7684\u5c42\u7ea7\u3002\u4ee5\u4e0b\u662f\u4ee3\u7801\u5b9e\u73b0\uff1a<\/p>\n<pre>a = 'a|[b|{c||d}|e]'\nc = {}\nnum = len(a)\nn = 0\nfind = 1\n\nwhile n &lt; num:\n    if (a[n] == \"[\") | (a[n] == \"{\"):\n        find += 1\n    if (a[n] == \"]\") | (a[n] == \"}\"):\n        find -= 1\n    if a[n] == '|':\n        d = c.setdefault(find, [n])\n        if d != [n]:\n            c[find].append(n)\n    n += 1<\/pre>\n<p><strong>\u8f93\u51fa<\/strong><\/p>\n<p>\u5b57\u5178 c \u4ee5\u5c42\u7ea7\u4e3a\u952e\uff0c\u7ad6\u7ebf\u4f4d\u7f6e\u4e3a\u503c\uff1a<\/p>\n<pre>{1: [1], 2: [4, 11], 3: [7, 8]}<\/pre>\n<p>\u952e\u8868\u793a\u5c42\u7ea7\uff0c\u800c\u503c\u8868\u793a\u6bcf\u4e2a\u5c42\u7ea7\u4e2d\u7ad6\u7ebf\u7684\u4f4d\u7f6e\u3002<\/p>\n<p><strong>\u5206\u89e3\u7ed3\u679c<\/strong><\/p>\n<p>\u5229\u7528\u5b57\u5178\u4e2d\u7684\u5c42\u7ea7\u4fe1\u606f\uff0c\u6211\u4eec\u53ef\u4ee5\u5c42\u5c42\u5206\u89e3\u5b57\u7b26\u4e32\uff1a<\/p>\n<pre>['A', 'B { C | D } | E']\n['A', ['B { C | D }', 'E']]\n['A', ['B', '[ C | D ]', 'E']]\n['A', ['B', ['C', 'D'], 'E']]<\/pre>\n<p>\u4eca\u5929\u5173\u4e8e\u300aPython \u5b57\u7b26\u4e32\u5c42\u7ea7\u89e3\u6790\uff1a\u5982\u4f55\u5224\u65ad\u4e0d\u540c\u5c42\u7ea7\u7684\u7ad6\u7ebf\u5206\u9694\u7b26\uff1f\u300b\u7684\u5185\u5bb9\u4ecb\u7ecd\u5c31\u5230\u6b64\u7ed3\u675f\uff0c\u5982\u679c\u6709\u4ec0\u4e48\u7591\u95ee\u6216\u8005\u5efa\u8bae\uff0c\u53ef\u4ee5\u5728\u516c\u4f17\u53f7\u4e0b\u591a\u591a\u56de\u590d\u4ea4\u6d41\uff1b\u6587\u4e2d\u82e5\u6709\u4e0d\u6b63\u4e4b\u5904\uff0c\u4e5f\u5e0c\u671b\u56de\u590d\u7559\u8a00\u4ee5\u544a\u77e5\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Python \u5b57\u7b26\u4e32\u5c42\u7ea7\u89e3\u6790\uff1a\u5982&#46;&#46;&#46;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4925],"tags":[],"class_list":["post-205403","post","type-post","status-publish","format-standard","hentry","category-4925"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/205403","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/comments?post=205403"}],"version-history":[{"count":0,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/205403\/revisions"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=205403"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=205403"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=205403"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}