{"id":45,"date":"2026-02-16T08:04:33","date_gmt":"2026-02-16T00:04:33","guid":{"rendered":"https:\/\/liuyunge.net\/?p=45"},"modified":"2026-02-20T20:33:11","modified_gmt":"2026-02-20T12:33:11","slug":"python%e4%b8%ad%e7%9a%84%e5%88%97%e8%a1%a8%e3%80%81%e5%ad%97%e5%85%b8","status":"publish","type":"post","link":"https:\/\/liuyunge.net\/index.php\/2026\/02\/16\/python%e4%b8%ad%e7%9a%84%e5%88%97%e8%a1%a8%e3%80%81%e5%ad%97%e5%85%b8\/","title":{"rendered":"python\u4e2d\u7684\u5e8f\u5217\uff1a\u5217\u8868\u3001\u5b57\u5178\u3001\u5143\u7ec4\u3001\u96c6\u5408"},"content":{"rendered":"\n<p id=\"\u9996\u9875\">\u5e8f\u5217<\/p>\n\n\n\n<p><a href=\"#\u5217\u8868\" data-type=\"internal\" data-id=\"#\u5217\u8868\">\u5217\u8868<\/a> <a href=\"#\u5b57\u5178\" data-type=\"internal\" data-id=\"#\u5b57\u5178\">\u5b57\u5178<\/a> <a href=\"#\u5143\u7ec4\" data-type=\"internal\" data-id=\"#\u5143\u7ec4\">\u5143\u7ec4<\/a> <a href=\"#\u96c6\u5408\" data-type=\"internal\" data-id=\"#\u96c6\u5408\">\u96c6\u5408<\/a><\/p>\n\n\n\n<p id=\"\u5217\u8868\"><a href=\"#\u5217\u8868\" data-type=\"internal\" data-id=\"#\u5217\u8868\">\u4e00\u3001\u5217\u8868\uff08List\uff09\uff1a\u6709\u5e8f\u3001\u53ef\u53d8\u3001\u53ef\u91cd\u590d\u7684\u5143\u7d20\u96c6\u5408<br>\u6838\u5fc3\u64cd\u4f5c\uff08\u589e\u5220\u6539\u67e5\uff09+ \u5e38\u7528\u51fd\u6570<\/a><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code># 1. \u5b9a\u4e49\u5217\u8868\nlst = &#91;1, 2, 3, \"a\", \"b\"]\nempty_lst = &#91;]\n# 2. \u67e5\uff08\u4e0b\u6807\/\u5207\u7247\/\u67e5\u627e\uff09\nprint(lst&#91;0]) # \u4e0b\u6807\u53d6\u503c\uff1a1\nprint(lst&#91;-1]) # \u8d1f\u4e0b\u6807\u53d6\u503c\uff1a\"b\"\nprint(lst&#91;1:4]) # \u5207\u7247\uff1a&#91;2, 3, \"a\"]\nprint(lst.index(\"a\")) # \u67e5\u627e\u5143\u7d20\u4e0b\u6807\uff1a3\nprint(lst.count(2)) # \u7edf\u8ba1\u5143\u7d20\u51fa\u73b0\u6b21\u6570\uff1a1\n# 3. \u589e\nlst.append(4) # \u672b\u5c3e\u52a0\u5143\u7d20\uff1a\n&#91;1,2,3,\"a\",\"b\",4]\nlst.extend(&#91;5,6]) # \u672b\u5c3e\u6279\u91cf\u52a0\uff1a\n&#91;1,2,3,\"a\",\"b\",4,5,6]\nlst.insert(2, \"x\") # \u6307\u5b9a\u4e0b\u6807\u63d2\u5165\uff1a\n&#91;1,2,\"x\",3,\"a\",\"b\",4,5,6]\n# 4. \u6539\nlst&#91;3] = 99 # \u4e0b\u6807\u4fee\u6539\uff1a\n&#91;1,2,\"x\",99,\"a\",\"b\",4,5,6]\nlst&#91;1:3] = &#91;88,77] # \u5207\u7247\u6279\u91cf\u4fee\u6539\uff1a\n&#91;1,88,77,99,\"a\",\"b\",4,5,6]\n# 5. \u5220\nlst.remove(\"a\") # \u5220\u9664\u6307\u5b9a\u5143\u7d20\uff1a\n&#91;1,88,77,99,\"b\",4,5,6]\nlst.pop() # \u5220\u9664\u6700\u540e\u4e00\u4e2a\u5143\u7d20\uff08\u8fd4\u56de\u503c\uff09\uff1a6 \u2192\n\u5217\u8868\u53d8\u4e3a&#91;1,88,77,99,\"b\",4,5]\nlst.pop(1) # \u5220\u9664\u6307\u5b9a\u4e0b\u6807\u5143\u7d20\uff1a88 \u2192\n&#91;1,77,99,\"b\",4,5]\ndel lst&#91;2] # \u5220\u9664\u6307\u5b9a\u4e0b\u6807\u5143\u7d20\uff1a99 \u2192\n&#91;1,77,\"b\",4,5]\nlst.clear() # \u6e05\u7a7a\u5217\u8868\uff1a&#91;]\n# 6. \u6392\u5e8f\/\u53cd\u8f6c\nlst = &#91;3,1,4,2]\nlst.sort() # \u5347\u5e8f\u6392\u5e8f\uff1a&#91;1,2,3,4]\nlst.sort(reverse=True) # \u964d\u5e8f\u6392\u5e8f\uff1a&#91;4,3,2,1]\nlst.reverse() # \u53cd\u8f6c\u5217\u8868\uff1a&#91;1,2,3,4]\nprint(sorted(lst)) # \u4e34\u65f6\u6392\u5e8f\uff08\u4e0d\u4fee\u6539\u539f\u5217\u8868\uff09\uff1a\n&#91;1,2,3,4]\n# 7. \u5176\u4ed6\u5e38\u7528\u51fd\u6570\nprint(len(lst)) # \u957f\u5ea6\uff1a4\nprint(max(lst)) # \u6700\u5927\u503c\uff1a4\uff08\u5143\u7d20\u7c7b\u578b\u9700\u4e00\u81f4\uff09\nprint(min(lst)) # \u6700\u5c0f\u503c\uff1a1\nprint(sum(lst)) # \u6c42\u548c\uff1a10\uff08\u4ec5\u6570\u5b57\u5217\u8868\uff09\n<a href=\"#\u9996\u9875\" data-type=\"internal\" data-id=\"#\u9996\u9875\">\u56de\u5230\u9996\u9875<\/a><\/code><\/pre>\n\n\n\n<p id=\"\u5b57\u5178\">\u4e8c\u3001\u5b57\u5178\uff08Dict\uff09\uff1a\u65e0\u5e8f\u3001\u952e\u552f\u4e00\u7684\u952e\u503c\u5bf9\u96c6\u5408<br>\u6838\u5fc3\u64cd\u4f5c\uff08\u589e\u5220\u6539\u67e5\uff09+ \u5e38\u7528\u51fd\u6570<\/p>\n\n\n\n<pre id=\"\u5143\u7ec4\" class=\"wp-block-code\"><code># 1. \u5b9a\u4e49\u5b57\u5178\ndic = {\"name\": \"\u5f20\u4e09\", \"age\": 20, \"score\": {\"\u8bed\u6587\":90, \"\u6570\u5b66\":85}}\nempty_dic = {}\n# 2. \u67e5\uff08\u53d6\u503c\/\u67e5\u952e\/\u503c\/\u952e\u503c\u5bf9\uff09\nprint(dic&#91;\"name\"]) # \u952e\u53d6\u503c\uff1a\"\u5f20\u4e09\"\nprint(dic.get(\"age\")) # get\u53d6\u503c\uff1a20\nprint(dic.get(\"gender\", \"\u7537\")) # \u952e\u4e0d\u5b58\u5728\u8fd4\u56de\u9ed8\u8ba4\u503c\uff1a\"\u7537\"\nprint(dic.keys()) # \u6240\u6709\u952e\uff1a\ndict_keys(&#91;'name','age','score'])\nprint(dic.values()) # \u6240\u6709\u503c\uff1a\ndict_values(&#91;'\u5f20\u4e09',20,{'\u8bed\u6587':90,'\u6570\u5b66':85}])\nprint(dic.items()) # \u6240\u6709\u952e\u503c\u5bf9\uff1a\ndict_items(&#91;('name','\u5f20\u4e09'),...])\n# 3. \u589e\/\u6539\ndic&#91;\"age\"] = 21 # \u952e\u5b58\u5728\u2192\u4fee\u6539\u503c\uff1a21\ndic&#91;\"gender\"] = \"\u7537\" # \u952e\u4e0d\u5b58\u5728\u2192\u65b0\u589e\uff1a\n{\"name\":\"\u5f20\u4e09\",\"age\":21,\"gender\":\"\u7537\",\"score\":...}\n<a href=\"#\u9996\u9875\" data-type=\"internal\" data-id=\"#\u9996\u9875\">\u56de\u5230\u9996\u9875<\/a><\/code><\/pre>\n\n\n\n<p id=\"\u5143\u7ec4\">\u4e09\u3001\u5143\u7ec4\uff08Tuple\uff09\uff1a\u6709\u5e8f\u3001\u53ef\u91cd\u590d\u7684\u5143\u7d20\u96c6\u5408<br>\u6838\u5fc3\u64cd\u4f5c\uff08\u4ec5\u67e5\/\u4e0d\u53ef\u6539\uff09+ \u5e38\u7528\u51fd\u6570<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code># 1. \u5b9a\u4e49\u5143\u7ec4\ntup = (1, 2, 3, \"a\", \"b\")\nsingle_tup = (5,) # \u5355\u4e2a\u5143\u7d20\u5fc5\u987b\u52a0\u9017\u53f7\uff0c\u5426\u5219\u89c6\u4e3a\u666e\u901a\u53d8\u91cf\nempty_tup = ()\n# 2. \u67e5\uff08\u4e0b\u6807\/\u5207\u7247\/\u67e5\u627e\uff09\nprint(tup&#91;0]) # \u4e0b\u6807\u53d6\u503c\uff1a1\nprint(tup&#91;-1]) # \u8d1f\u4e0b\u6807\u53d6\u503c\uff1a\"b\"\nprint(tup&#91;1:4]) # \u5207\u7247\uff1a(2, 3, \"a\")\nprint(tup.index(\"a\")) # \u67e5\u627e\u5143\u7d20\u4e0b\u6807\uff1a3\nprint(tup.count(2)) # \u7edf\u8ba1\u5143\u7d20\u51fa\u73b0\u6b21\u6570\uff1a1\n# 3. \u4e0d\u53ef\u4fee\u6539\uff08\u4ee5\u4e0b\u64cd\u4f5c\u62a5\u9519\uff0c\u4ec5\u4f5c\u5bf9\u6bd4\uff09\n# tup&#91;2] = 99 # \u62a5\u9519\uff1aTypeError\uff08\u5143\u7ec4\u4e0d\u53ef\u53d8\uff09\n# tup.append(4) # \u62a5\u9519\uff1aAttributeError\n# 4. \u5e38\u7528\u51fd\u6570\nprint(len(tup)) # \u957f\u5ea6\uff1a5\nprint(max(tup&#91;:3])) # \u6700\u5927\u503c\uff1a3\uff08\u4ec5\u6570\u5b57\u90e8\u5206\uff09\nprint(min(tup&#91;:3])) # \u6700\u5c0f\u503c\uff1a1\nprint(sum(tup&#91;:3])) # \u6c42\u548c\uff1a6\ntup_copy = tup&#91;:] # \u5207\u7247\u62f7\u8d1d\u5143\u7ec4\n# 5. \u5143\u7ec4\u62c6\u5305\na, b,\n*rest = tup # \u62c6\u5305\uff1aa=1, b=2, rest=\n&#91;3,\"a\",\"b\"]<\/code><\/pre>\n\n\n\n<p><a href=\"#\u9996\u9875\" data-type=\"internal\" data-id=\"#\u9996\u9875\">\u56de\u5230\u9996\u9875<\/a><\/p>\n\n\n\n<p id=\"\u96c6\u5408\">\u56db\u3001\u96c6\u5408\uff08Set\uff09\uff1a\u65e0\u5e8f\u3001\u5143\u7d20\u552f\u4e00\u7684\u96c6\u5408<br>\u6838\u5fc3\u64cd\u4f5c\uff08\u589e\u5220\/\u53bb\u91cd\/\u96c6\u5408\u8fd0\u7b97\uff09+ \u5e38\u7528\u51fd\u6570<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\u6838\u5fc3\u64cd\u4f5c\uff08\u589e\u5220\/\u53bb\u91cd\/\u96c6\u5408\u8fd0\u7b97\uff09+ \u5e38\u7528\u51fd\u6570\n# 1. \u5b9a\u4e49\u96c6\u5408\ns = {1, 2, 3, 3, \"a\"} # \u81ea\u52a8\u53bb\u91cd\uff1a{1,2,3,\"a\"}\nempty_set = set() # \u7a7a\u96c6\u5408\u5fc5\u987b\u7528set()\uff0c\u4e0d\u80fd\u7528{}\uff08{}\u662f\u5b57\u5178\uff09\n# 2. \u67e5\uff08\u65e0\u4e0b\u6807\uff0c\u53ea\u80fd\u904d\u5386\/\u5224\u65ad\uff09\nprint(2 in s) # \u5224\u65ad\u5143\u7d20\u662f\u5426\u5b58\u5728\uff1aTrue\nfor elem in s:\n  print(elem) # \u904d\u5386\uff1a1,2,3,\"a\"\uff08\u987a\u5e8f\u968f\u673a\uff09\n# 3. \u589e\ns.add(4) # \u6dfb\u52a0\u5355\u4e2a\u5143\u7d20\uff1a{1,2,3,4,\"a\"}\ns.update(&#91;5,6]) # \u6279\u91cf\u6dfb\u52a0\uff1a{1,2,3,4,5,6,\"a\"}\n# 4. \u5220\ns.remove(3) # \u5220\u9664\u6307\u5b9a\u5143\u7d20\uff08\u4e0d\u5b58\u5728\u62a5\u9519\uff09\uff1a\n{1,2,4,5,6,\"a\"}\ns.discard(9) # \u5220\u9664\u6307\u5b9a\u5143\u7d20\uff08\u4e0d\u5b58\u5728\u4e0d\u62a5\u9519\uff09\ns.pop() # \u968f\u673a\u5220\u9664\u4e00\u4e2a\u5143\u7d20\uff08\u8fd4\u56de\u503c\uff09\ns.clear() # \u6e05\u7a7a\u96c6\u5408\uff1aset()\n# 5. \u96c6\u5408\u8fd0\u7b97\uff08\u4ea4\u96c6\/\u5e76\u96c6\/\u5dee\u96c6\/\u5bf9\u79f0\u5dee\u96c6\uff09\ns1 = {1,2,3,4}\ns2 = {3,4,5,6}\nprint(s1 &amp; s2) # \u4ea4\u96c6\uff1a{3,4}\nprint(s1 | s2) # \u5e76\u96c6\uff1a{1,2,3,4,5,6}\nprint(s1 - s2) # \u5dee\u96c6\uff08s1\u6709s2\u65e0\uff09\uff1a{1,2}\nprint(s1 ^ s2) # \u5bf9\u79f0\u5dee\u96c6\uff08\u4ec5\u4e00\u65b9\u6709\uff09\uff1a{1,2,5,6}\n# 6. \u5176\u4ed6\u5e38\u7528\u51fd\u6570\nprint(len(s1)) # \u957f\u5ea6\uff1a4\nprint(max(s1)) # \u6700\u5927\u503c\uff1a4\nprint(min(s1)) # \u6700\u5c0f\u503c\uff1a1\ns_copy = s1.copy() # \u62f7\u8d1d\u96c6\u5408\n# 7. \u53bb\u91cd\u5b9e\u7528\u573a\u666f\nlst = &#91;1,2,2,3,3,3]\nunique_lst = list(set(lst)) # \u5217\u8868\u53bb\u91cd\uff1a&#91;1,2,3]\uff08\u987a\u5e8f\u968f\u673a\uff09<\/code><\/pre>\n\n\n\n<p><a href=\"#\u9996\u9875\" data-type=\"internal\" data-id=\"#\u9996\u9875\">\u56de\u5230\u9996\u9875<\/a><\/p>\n\n\n\n<p><strong>\u6838\u5fc3\u603b\u7ed3<\/strong><\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"695\" src=\"https:\/\/liuyunge.net\/wp-content\/uploads\/2026\/02\/image-1024x695.png\" alt=\"\" class=\"wp-image-46\" srcset=\"https:\/\/liuyunge.net\/wp-content\/uploads\/2026\/02\/image-1024x695.png 1024w, https:\/\/liuyunge.net\/wp-content\/uploads\/2026\/02\/image-300x204.png 300w, https:\/\/liuyunge.net\/wp-content\/uploads\/2026\/02\/image-768x521.png 768w, https:\/\/liuyunge.net\/wp-content\/uploads\/2026\/02\/image-398x270.png 398w, https:\/\/liuyunge.net\/wp-content\/uploads\/2026\/02\/image.png 1300w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p><\/p>\n\n\n\n<p>\u6240\u6709\u64cd\u4f5c\u5747\u9075\u5faa\u201c\u5217\u8868\/\u5b57\u5178\/\u96c6\u5408\u53ef\u53d8\uff0c\u5143\u7ec4\u4e0d\u53ef\u53d8\u201d\u7684\u6838\u5fc3\u89c4\u5219\uff0c<br>\u51fd\u6570\u4f7f\u7528\u65f6\u6ce8\u610f\uff1a<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li>\u53ef\u53d8\u7c7b\u578b\uff08\u5217\u8868\/\u5b57\u5178\/\u96c6\u5408\uff09\u7684\u4fee\u6539\u64cd\u4f5c\u76f4\u63a5\u6539\u53d8\u539f\u5bf9\u8c61\uff1b<\/li>\n\n\n\n<li>\u4e0d\u53ef\u53d8\u7c7b\u578b\uff08\u5143\u7ec4\/\u5b57\u7b26\u4e32\uff09\u7684\u64cd\u4f5c\u5747\u8fd4\u56de\u65b0\u5bf9\u8c61\uff1b<\/li>\n\n\n\n<li>\u96c6\u5408\u65e0\u4e0b\u6807\uff0c\u4e0d\u80fd\u901a\u8fc7\u4e0b\u6807\u8bbf\u95ee\uff0c\u4ec5\u80fd\u901a\u8fc7\u201c\u5143\u7d20\u662f\u5426\u5b58\u5728\u201d\u5224\u65ad\u3002<\/li>\n<\/ol>\n\n\n\n<pre class=\"wp-block-code\"><code>\n<a href=\"#\u9996\u9875\" data-type=\"internal\" data-id=\"#\u9996\u9875\">\u56de\u5230\u9996\u9875<\/a>\n\n#1\u3001python\u4e09\u79cd\u57fa\u672c\u7c7b\u578b\u7684\u8f6c\u6362\nint('123')\nfloat(123)\nstr(123)\n\n#2\u3001\u628a\u6574\u6570\u62fc\u63a5\u6210\u5b57\u7b26\u4e32\na = 23\nb = 24\nc = 25\nprint(str(a) + str(b)+str(c))\n\n#3\u3001\u628a\u5b57\u7b26\u4e32\u8f6c\u6362\u6210\u5217\u8868\n# \u7b2c\u4e00\u79cd\u65b9\u5f0f\nsl = '1,2,3'\nprint(sl.split(',')) # \u8f93\u51fa\u7ed3\u679c&#91;'1', '2', '3']\n\n# \u7b2c\u4e8c\u79cd\u65b9\u5f0f\ns = \"&#91;1,2,'\u5218\u4ea6\u83f2']\"\nb = eval(s)\nprint(b)\nprint(type(b))\n\n# 4\u3001\u628a\u5217\u8868\u8f6c\u6362\u6210\u5b57\u7b26\u4e32\n\nd = &#91;'1','2','3']\ne = ''.join(d)\nprint(e)\nprint(type(e))\n\n# 5\u3001\u628a\u5217\u8868\u8f6c\u6362\u4e3a\u5b57\u5178\nlist_name = &#91;'\u5c0f\u7ea2','\u5c0f\u4eae']\nlist_score = &#91;80,90]\ndict_scroe = zip(list_name,list_score)\ndict_score_res= dict(dict_scroe)\nprint(dict_score_res)\n\n# 6\u3001\u5c06\u5b57\u5178\u8f6c\u6362\u6210\u5217\u8868\n\ndict_name_score ={'\u5c0f\u7ea2': 80, '\u5c0f\u4eae': 90}\n\nlist_name =list(dict_name_score.keys())\nprint(list_name)\n\nlist_socre = list(dict_name_score.values())\nprint(list_score)<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u5e8f\u5217 \u5217\u8868 \u5b57\u5178 \u5143\u7ec4 \u96c6\u5408 \u4e00\u3001\u5217\u8868\uff08List\uff09\uff1a\u6709\u5e8f\u3001\u53ef\u53d8\u3001\u53ef\u91cd\u590d\u7684\u5143\u7d20\u96c6\u5408\u6838\u5fc3\u64cd\u4f5c\uff08\u589e\u5220\u6539\u67e5\uff09+ \u5e38\u7528\u51fd\u6570 \u4e8c\u3001\u5b57\u5178\uff08Dict\uff09\uff1a\u65e0\u5e8f\u3001\u952e\u552f\u4e00\u7684\u952e\u503c\u5bf9\u96c6\u5408&#8230;<\/p>\n<p class=\"read-more\"><a class=\"btn btn-default\" href=\"https:\/\/liuyunge.net\/index.php\/2026\/02\/16\/python%e4%b8%ad%e7%9a%84%e5%88%97%e8%a1%a8%e3%80%81%e5%ad%97%e5%85%b8\/\"> Read More<span class=\"screen-reader-text\">  Read More<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-45","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/liuyunge.net\/index.php\/wp-json\/wp\/v2\/posts\/45","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/liuyunge.net\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/liuyunge.net\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/liuyunge.net\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/liuyunge.net\/index.php\/wp-json\/wp\/v2\/comments?post=45"}],"version-history":[{"count":3,"href":"https:\/\/liuyunge.net\/index.php\/wp-json\/wp\/v2\/posts\/45\/revisions"}],"predecessor-version":[{"id":49,"href":"https:\/\/liuyunge.net\/index.php\/wp-json\/wp\/v2\/posts\/45\/revisions\/49"}],"wp:attachment":[{"href":"https:\/\/liuyunge.net\/index.php\/wp-json\/wp\/v2\/media?parent=45"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/liuyunge.net\/index.php\/wp-json\/wp\/v2\/categories?post=45"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/liuyunge.net\/index.php\/wp-json\/wp\/v2\/tags?post=45"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}