{"id":53,"date":"2026-05-13T00:50:35","date_gmt":"2026-05-12T16:50:35","guid":{"rendered":"http:\/\/w.renho.local\/?p=53"},"modified":"2026-05-13T01:36:27","modified_gmt":"2026-05-12T17:36:27","slug":"python%e5%9f%ba%e7%a1%80","status":"publish","type":"post","link":"https:\/\/www.devfm.cn\/?p=53","title":{"rendered":"python\u57fa\u7840"},"content":{"rendered":"<h3>\u96c6\u5408<\/h3>\n<pre><code class=\"language-python line-numbers\"># set\n\ns1 = {1, 2, 3, 4}\nprint(type(s1))\nprint(s1)\ns1.add(5)\ns1.add(6)\nprint(s1)\ns2 = {5, 6, 7, 8}\ns3 = s1 | s2\nprint(s3)\ns3.remove(3)\ns3.discard(4)\nprint(s3)\n\ns4 = {s for s in range(10, 20)}\nprint(s4)\n\n\nt0 = (1, 2, 3, 4)\nprint(t0)\n\n\nm = {\n    \"a\": \"a\",\n    \"b\": \"\",\n    \"c\": None\n}\n\nprint(m.get(\"a\"))\nprint(m.get(\"b\"))\nprint(m.get(\"c\"))\nprint(m.get(\"d\"))\n\n\nprint(\"--------\")\nprint(m.get(\"a\", \"aa\"))\nprint(m.get(\"b\", \"bb\"))\nprint(m.get(\"c\", \"cc\"))\nprint(m.get(\"d\", \"dd\"))\nprint(\"--------\")\nprint(m.get(\"a\") if m.get(\"a\") else \"aa\")\nprint(m.get(\"b\") if m.get(\"b\") else \"bb\")\nprint(m.get(\"c\") if m.get(\"c\") else \"cc\")\nprint(m.get(\"d\") if m.get(\"d\") else \"dd\")\n<\/code><\/pre>\n<h3>\u5185\u7f6e\u51fd\u6570<\/h3>\n<pre><code class=\"language-python line-numbers\">################## zip\nlist0 = [\"a\", \"b\", \"c\"]\nlist1 = [1, 2, 3]\nlist2 = [\"A\", \"B\", \"C\"]\n\nresult = zip(list0, list1, list2)\nprint(str(result))\n# for item in result:\n#     print(item)\nlist3 = list(result)\nprint(list3)\nprint('----------')\nprint(locals())\nprint(globals())\n\n\n################## sorted\nlist0 = [\"a\", \"abb\", \"adddd\", \"accc\"]\nresult = sorted(list0, key=lambda k: len(k))\nprint(result)\n\n################## filter\nresult = filter(lambda s: \"adddd\" == s, list0)\nprint(list(result))\n\n################## map\nresult = map(lambda i : i + i, list0)\nprint(list(result))\n\n################## reduce\ns0 = {0, 1, 2, 3}\ns1 = {1, 2, 3}\nfrom functools import reduce\nresult = reduce(lambda a, b: a | b, [s0, s1])\n\nprint('67' in '67@1@dhxy')\n\n################## zip\nlist0 = [\"a\", \"b\", \"c\"]\nlist1 = [1, 2, 3]\nlist2 = [\"A\", \"B\", \"C\"]\n\nresult = zip(list0, list1, list2)\nprint(str(result))\n# for item in result:\n#     print(item)\nlist3 = list(result)\nprint(list3)\nprint('----------')\nprint(locals())\nprint(globals())\n\n\n################## sorted\nlist0 = [\"a\", \"abb\", \"adddd\", \"accc\"]\nresult = sorted(list0, key=lambda k: len(k))\nprint(result)\n\n################## filter\nresult = filter(lambda s: \"adddd\" == s, list0)\nprint(list(result))\n\n################## map\nresult = map(lambda i : i + i, list0)\nprint(list(result))\n\n################## reduce\ns0 = {0, 1, 2, 3}\ns1 = {1, 2, 3}\nfrom functools import reduce\nresult = reduce(lambda a, b: a | b, [s0, s1])\nprint(result)\n<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u96c6\u5408 # set s1 = {1, 2, 3, 4} print(type(s1)) print(s1) s1&hellip; <a class=\"read-more-link\" href=\"https:\/\/www.devfm.cn\/?p=53\">Read More<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[5],"tags":[],"class_list":["post-53","post","type-post","status-publish","format-standard","hentry","category-python"],"_links":{"self":[{"href":"https:\/\/www.devfm.cn\/index.php?rest_route=\/wp\/v2\/posts\/53","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.devfm.cn\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.devfm.cn\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.devfm.cn\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.devfm.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=53"}],"version-history":[{"count":3,"href":"https:\/\/www.devfm.cn\/index.php?rest_route=\/wp\/v2\/posts\/53\/revisions"}],"predecessor-version":[{"id":63,"href":"https:\/\/www.devfm.cn\/index.php?rest_route=\/wp\/v2\/posts\/53\/revisions\/63"}],"wp:attachment":[{"href":"https:\/\/www.devfm.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=53"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.devfm.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=53"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.devfm.cn\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=53"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}