{"id":12982,"date":"2023-04-12T14:27:03","date_gmt":"2023-04-12T06:27:03","guid":{"rendered":"https:\/\/blog.ywdevops.cn\/?p=12982"},"modified":"2025-07-24T15:12:17","modified_gmt":"2025-07-24T07:12:17","slug":"python-17","status":"publish","type":"post","link":"https:\/\/blog.ywdevops.cn\/index.php\/2023\/04\/12\/python-17\/","title":{"rendered":"Python\u5e38\u7528\u51fd\u6570\u548c\u65b9\u6cd5"},"content":{"rendered":"\n<ul class=\"wp-block-list\">\n<li>list()\uff1a\u7528\u4e8e\u5c06\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3a\u5217\u8868<\/li>\n\n\n\n<li>reversed()\uff1a\u5bf9\u5217\u8868\u5012\u7740\u6253\u5370\uff0c\u6bd4\u5982[0,1,2]\uff0c\u6253\u5370\u540e\u5c31\u4e3a[2,1,0],\u7528\u6cd5list.reversed()\uff0c\u4e0d\u80fd\u7528\u4e8e\u96c6\u5408<\/li>\n\n\n\n<li>random.random(): random\u5e93\u7684random()\u51fd\u6570\u8fd4\u56de\u968f\u673a\u751f\u6210\u7684\u4e00\u4e2a\u6d6e\u70b9\u6570\uff0c\u8303\u56f4\u5728[0,1)\u4e4b\u95f4<\/li>\n\n\n\n<li>random.uniform(a, b): \u8fd4\u56de\u968f\u673a\u751f\u6210\u7684\u4e00\u4e2a\u6d6e\u70b9\u6570\uff0c\u8303\u56f4\u5728[a, b)\u4e4b\u95f4<\/li>\n\n\n\n<li>random.randint(a,b)\uff1a\u751f\u6210\u6307\u5b9a\u8303\u56f4\u5185\u7684\u6574\u6570<\/li>\n\n\n\n<li>random.choice()\uff1a\u4ece\u6307\u5b9a\u7684\u5e8f\u5217\u4e2d\u83b7\u53d6\u4e00\u4e2a\u968f\u673a\u5143\u7d20<\/li>\n\n\n\n<li>range()\uff1a\u7528\u4e8e\u751f\u6210\u4e00\u7cfb\u5217\u6574\u6570\uff0c\u4e00\u822c\u7528\u4e8efor\u5faa\u73af\u4e2d \u4f8b\uff1afor i in range(10)<\/li>\n\n\n\n<li>sort()\uff1a\u5bf9\u5217\u8868\u8fdb\u884c\u6392\u5e8f\uff0c\u4f1a\u6539\u53d8\u539f\u5217\u8868\u987a\u5e8f\uff0c\u4fee\u6539\u7684\u662f\u539f\u5217\u8868\u800c\u4e0d\u662f\u8fd4\u56de\u65b0\u5217\u8868,list.sort()<\/li>\n\n\n\n<li>sort(reverse=True)\uff1a\u6309\u7167\u76f8\u53cd\u65b9\u5411\u6392\u5e8f\uff0c\u4e0esort()\u662f\u53cd\u7740\u6765\u7684,list.sort(reverse=True)<\/li>\n\n\n\n<li>sorted()\uff1a\u5bf9\u5217\u8868\u3001\u5143\u7ec4\u3001\u5b57\u5178\u3001\u96c6\u5408\u3001\u8fd8\u5305\u62ec\u5b57\u7b26\u4e32\u8fdb\u884c\u6392\u5e8f\uff0c\u4e0d\u6539\u53d8\u539f\u987a\u5e8f,\u8fd4\u56de\u65b0\u503c\uff0csorted(list)<\/li>\n\n\n\n<li>sorted(reverse=True)\uff1a\u53cd\u7740\u6765\u6392\u5e8f\uff0c\u4e0esorted\u7ed3\u679c\u76f8\u53cd<\/li>\n\n\n\n<li>sum()\uff1a\u5bf9\u8fed\u4ee3\u5bf9\u8c61\u6c42\u548c\uff0c\u53ef\u8fed\u4ee3\u5bf9\u8c61\u4e3a\u5217\u8868\u3001\u5143\u7ec4\u3001\u96c6\u5408\u3001\u5b57\u5178\u7b49<\/li>\n\n\n\n<li>count()\uff1a\u83b7\u53d6\u5143\u7d20\u51fa\u73b0\u7684\u6b21\u6570,\u4f8b\uff1alist.count(&#8220;hello&#8221;)<\/li>\n\n\n\n<li>remove()\uff1a\u5bf9\u5217\u8868\u5143\u7d20\u5220\u9664\uff0c\u4f8b\uff1alist.remove(&#8220;hello&#8221;)<\/li>\n\n\n\n<li>extend()\uff1a\u5c06\u539f\u5217\u8868\u8ffd\u52a0\u5230\u65b0\u5217\u8868\u4e2d \u4f8b\uff1anewlist.extend(oldlist)<\/li>\n\n\n\n<li>append()\uff1a\u5411\u5217\u8868\u4e2d\u6dfb\u52a0\u5143\u7d20  \u4f8b\uff1alist.append(&#8220;hello&#8221;)<\/li>\n\n\n\n<li>type()\uff1a\u67e5\u770b\u53d8\u91cf\u7c7b\u578b \u4f8b\uff1a type(&#8220;hello&#8221;)<\/li>\n\n\n\n<li>tuple()\uff1a\u5c06\u53ef\u8fed\u4ee3\u5e8f\u5217\u8f6c\u6362\u4e3a\u5143\u7ec4 \u4f8b\uff1a tuple(list)  \u5c06\u5217\u8868\u8f6c\u4e3a\u5143\u7ec4<\/li>\n\n\n\n<li>print()\uff1a\u6253\u5370\u5185\u5bb9<\/li>\n\n\n\n<li>enumerate()\uff1a\u904d\u5386\u6570\u7ec4\u6216\u8005\u5143\u7ec4\u7684\u65f6\u5019\u53ef\u4ee5\u83b7\u53d6\u5230\u7d22\u5f15\uff0c\u4e00\u822c\u7528\u4e8efor\u5faa\u73af<\/li>\n\n\n\n<li>insert()\uff1a\u5411\u5217\u8868\u4e2d\u6307\u5b9a\u4f4d\u7f6e\u6dfb\u52a0\u5143\u7d20\uff0c\u4e0d\u5e38\u7528<\/li>\n\n\n\n<li>dict()\uff1a\u521b\u5efa\u4e00\u4e2a\u7a7a\u5b57\u5178\uff0c\u4f8b\uff1aa = dict()<\/li>\n\n\n\n<li>zip()\uff1a\u5c06\u591a\u4e2a\u5217\u8868\u6216\u5143\u7ec4\u4e2d\u5bf9\u5e94\u4f4d\u7f6e\u7684\u5143\u7d20\u7ed3\u5408\u4e3a\u5143\u7ec4\uff0c\u5e76\u8fd4\u56de\u5305\u542b\u8fd9\u4e9b\u5185\u5bb9\u7684zip\u5bf9\u8c61<\/li>\n\n\n\n<li>list.pop()\uff1a\u5217\u8868\u8c03\u7528pop()\u51fd\u6570\u8868\u793a\u5220\u9664\u6700\u540e\u4e00\u4e2a\u5143\u7d20<\/li>\n\n\n\n<li>list.pop(1)\uff1a\u5220\u9664\u5217\u8868\u6307\u5b9a\u7d22\u5f15\u7684\u5143\u7d20<\/li>\n\n\n\n<li>dict.pop(&#8220;hello&#8221;)\uff1a\u5220\u9664\u5b57\u6bb5\u6307\u5b9a\u7684key<\/li>\n\n\n\n<li>popitem()\uff1a\u5220\u9664\u5b57\u6bb5\u7684\u6700\u540e\u4e00\u4e2a\u5143\u7d20 \u4f8b\uff1adict.popitem()<\/li>\n\n\n\n<li>get()\uff1a\u83b7\u53d6\u5b57\u5178\u6307\u5b9akey\u7684value \u4f8b\uff1adict.get(&#8220;key&#8221;)<\/li>\n\n\n\n<li>items()\uff1a\u83b7\u53d6\u5b57\u5178\u7684\u952e\u503c\u5bf9\u5217\u8868 \u4f8b\uff1adict.items()<\/li>\n\n\n\n<li>json.dump(dict\uff0cfile) : \u5c06dict\u4ee5json\u683c\u5f0f\u5b58\u50a8\u5230file\u4e2d,\u4e0d\u4e00\u5b9a\u975e\u662fdict\uff0clist\u4e5f\u53ef\u4ee5,dict\u66f4\u7b26\u5408json\u683c\u5f0f<\/li>\n\n\n\n<li>json.dumps(dict)\uff1a\u5c06\u5b57\u5178\u8f6c\u6362\u4e3a\u5b57\u7b26\u4e32\uff0c\u5b9e\u9645\u8f6c\u6362\u540e\u4e3ajson\u683c\u5f0f\uff0c\u8fd4\u56de\u5b57\u7b26\u4e32<\/li>\n\n\n\n<li>json.load(f)\uff1a\u8bfb\u53d6\u6253\u5f00\u7684\u6587\u4ef6\u5bf9\u8c61\uff0c\u83b7\u53d6\u5185\u5bb9<\/li>\n\n\n\n<li>json.loads(str)\uff1a\u5c06json\u6570\u636e\u8f6c\u6362\u4e3a\u5b57\u5178\uff0cstr\u8868\u793ajson\u6570\u636e<\/li>\n\n\n\n<li>add()\uff1a\u5411\u96c6\u5408\u4e2d\u6dfb\u52a0\u5143\u7d20 \u4f8b\uff1asetname.add(&#8220;hello&#8221;)<\/li>\n\n\n\n<li>setname.pop()\uff1a\u96c6\u5408\u8c03\u7528pop()\u8868\u793a\u968f\u673a\u5220\u9664\u96c6\u5408\u4e2d\u4e00\u4e2a\u5143\u7d20<\/li>\n\n\n\n<li>setname.remove()\uff1a\u79fb\u9664\u96c6\u5408\u4e2d\u7684\u5143\u7d20 \u4f8b\uff1a setname.remove(&#8220;hello&#8221;)<\/li>\n\n\n\n<li>encode()\uff1a\u5c06\u5b57\u7b26\u4e32\u8fdb\u884c\u7f16\u7801\uff0c\u9ed8\u8ba4\u7f16\u7801\u4e3autf8 \u4f8b\uff1avar.encode()<\/li>\n\n\n\n<li>encode(&#8216;gbk&#8217;)\uff1a\u901a\u8fc7gbk\u7f16\u7801\u5b57\u7b26\u4e32 \u4f8b\uff1avar.encode(&#8216;gbk&#8217;)<\/li>\n\n\n\n<li>split()\uff1a\u5c06\u5b57\u7b26\u4e32\u8fdb\u884c\u5206\u5272\u4e3a\u5b57\u7b26\u4e32\u5217\u8868 \u4f8b\uff1avar.split()\uff0c\u9ed8\u8ba4\u5206\u9694\u7b26\u4e3a\u7a7a\u683c<\/li>\n\n\n\n<li>join()\uff1a\u5c06\u5b57\u7b26\u4e32\u5217\u8868\u8f6c\u6362\u4e3a\u5b57\u7b26\u4e32\uff0c\u4f8b\u5982\uff1a@.join(list)\uff0c\u5b57\u7b26\u4e32\u4ee5@\u8fde\u63a5\uff0c\u4e5f\u53ef\u4ee5\u4e3a&#8221;<\/li>\n\n\n\n<li>find()\uff1a\u68c0\u7d22\u662f\u5426\u5305\u542b\u6307\u5b9a\u5b57\u7b26\u4e32\uff0c\u5305\u542b\u8fd4\u56de\u5bf9\u5e94\u7d22\u5f15\uff0c\u4e0d\u5305\u542b\u8fd4\u56de-1 \u4f8b\uff1astr.find(&#8220;hello&#8221;)<\/li>\n\n\n\n<li>startswith()\uff1a\u68c0\u7d22\u5b57\u7b26\u4e32\u662f\u5426\u4ee5\u6307\u5b9a\u5b57\u7b26\u4e32\u5f00\u5934 \u4f8b\uff1avar.startswith(&#8220;hello&#8221;)<\/li>\n\n\n\n<li>endswith()\uff1a\u68c0\u7d22\u5b57\u7b26\u4e32\u662f\u5426\u4ee5\u6307\u5b9a\u5b57\u7b26\u4e32\u7ed3\u5c3e  \u4f8b\uff1avar.startswith(&#8220;hello&#8221;)<\/li>\n\n\n\n<li>lower()\uff1a\u5c06\u5b57\u7b26\u4e32\u4e2d\u7684\u6240\u6709\u5927\u5199\u5b57\u6bcd\u8f6c\u6362\u4e3a\u5c0f\u5199\u5b57\u6bcd \u4f8b\uff1a var.lower()<\/li>\n\n\n\n<li>upper()\uff1a\u5c06\u5b57\u7b26\u4e32\u4e2d\u7684\u6240\u6709\u5c0f\u5199\u5b57\u6bcd\u8f6c\u6362\u4e3a\u5927\u5199\u5b57\u6bcd \u4f8b\uff1a var.upper()<\/li>\n\n\n\n<li>strip()\uff1a\u53bb\u9664\u5b57\u7b26\u4e32\u5de6\u53f3\u4e24\u4fa7\u7a7a\u683c\u548c\u7279\u6b8a\u5b57\u7b26\uff0c\u7279\u6b8a\u5b57\u7b26\u662f\u6307\u5236\u8868\u7b26\\t\uff0c\u56de\u8f66\u7b26\\r\uff0c\u6362\u884c\u7b26\\n<\/li>\n\n\n\n<li>rstrip()\uff1a\u53bb\u6389\u5b57\u7b26\u4e32\u53f3\u4fa7\u7684\u7a7a\u683c\uff0c\u7528\u6cd5str.rstrip()<\/li>\n\n\n\n<li>lstrip()\uff1a\u53bb\u6389\u5b57\u7b26\u4e32\u5de6\u4fa7\u7684\u7a7a\u683c\uff0c\u7528\u6cd5 str.lstrip()<\/li>\n\n\n\n<li>format()\uff1a\u683c\u5f0f\u5316\u5b57\u7b26\u4e32<\/li>\n\n\n\n<li>open()\uff1a\u6253\u5f00\u6587\u4ef6<\/li>\n\n\n\n<li>close()\uff1a\u5173\u95ed\u6587\u4ef6<\/li>\n\n\n\n<li>write()\uff1a\u5411\u6587\u4ef6\u4e2d\u5199\u5165\u6570\u636e<\/li>\n\n\n\n<li>read(size)\uff1a\u8bfb\u53d6\u6307\u5b9a\u4e2a\u6570\u7684\u5b57\u7b26\uff0c\u8fd4\u56de\u5b57\u7b26\u4e32\uff0c\u4e0d\u52a0size\u8868\u793a\u8bfb\u53d6\u5168\u90e8<\/li>\n\n\n\n<li>readline()\uff1a\u9010\u884c\u8bfb\u53d6\uff0c\u8fd4\u56de\u5b57\u7b26\u4e32<\/li>\n\n\n\n<li>readlines()\uff1a\u5168\u90e8\u8bfb\u53d6\uff0c\u8fd4\u56de\u4e00\u4e2a\u5b57\u7b26\u4e32\u5217\u8868<\/li>\n\n\n\n<li>pprint()\uff1aPython\u7ed3\u6784\u5316\u6253\u5370\u51fd\u6570\uff0c\u6253\u5370\u51fa\u6765\u6613\u8bfb<\/li>\n\n\n\n<li>escape()\uff1a\u8f6c\u4e49\u51fd\u6570<\/li>\n\n\n\n<li>title()\uff1a\u5c06\u5b57\u7b26\u4e32\u4e2d\u6bcf\u4e2a\u5355\u8bcd\u9996\u5b57\u6bcd\u8f6c\u6362\u4e3a\u5927\u5199<\/li>\n\n\n\n<li>len()\uff1a\u83b7\u53d6\u5217\u8868\u7684\u957f\u5ea6\uff0c\u7528\u6cd5len(list)<\/li>\n\n\n\n<li>min()\uff1a\u627e\u51fa\u6570\u5b57\u5217\u8868\u7684\u6700\u5c0f\u503c<\/li>\n\n\n\n<li>max()\uff1a\u627e\u51fa\u6570\u5b57\u5217\u8868\u7684\u6700\u5927\u503c<\/li>\n\n\n\n<li>sun()\uff1a\u6c42\u6570\u5b57\u5217\u8868\u7684\u603b\u548c<\/li>\n\n\n\n<li>input()\uff1a\u83b7\u53d6\u7ec8\u7aef\u8f93\u5165\uff0c\u8f93\u5165\u7684\u5185\u5bb9\u90fd\u5c06\u88ab\u89e3\u8bfb\u4e3a\u5b57\u7b26\u4e32\uff0c\u8f93\u5165100\u8fd4\u56de\u201c100\u201d<\/li>\n\n\n\n<li>int()\uff1a\u83b7\u53d6\u7ec8\u7aef\u8f93\u5165\uff0c\u5e76\u53ef\u5c06\u8f93\u5165\u7684\u5185\u5bb9\u89e3\u8bfb\u4e3a\u6570\u503c\uff0c\u7528\u6cd5int(&#8220;100&#8221;)\u5c06\u8fd4\u56de100<\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"","protected":false},"author":3,"featured_media":12983,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[38,29],"tags":[],"class_list":["post-12982","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-python","category-yunwei"],"blocksy_meta":{"styles_descriptor":{"styles":{"desktop":"","tablet":"","mobile":""},"google_fonts":[],"version":8}},"_links":{"self":[{"href":"https:\/\/blog.ywdevops.cn\/index.php\/wp-json\/wp\/v2\/posts\/12982","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blog.ywdevops.cn\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.ywdevops.cn\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.ywdevops.cn\/index.php\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.ywdevops.cn\/index.php\/wp-json\/wp\/v2\/comments?post=12982"}],"version-history":[{"count":62,"href":"https:\/\/blog.ywdevops.cn\/index.php\/wp-json\/wp\/v2\/posts\/12982\/revisions"}],"predecessor-version":[{"id":18744,"href":"https:\/\/blog.ywdevops.cn\/index.php\/wp-json\/wp\/v2\/posts\/12982\/revisions\/18744"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/blog.ywdevops.cn\/index.php\/wp-json\/wp\/v2\/media\/12983"}],"wp:attachment":[{"href":"https:\/\/blog.ywdevops.cn\/index.php\/wp-json\/wp\/v2\/media?parent=12982"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.ywdevops.cn\/index.php\/wp-json\/wp\/v2\/categories?post=12982"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.ywdevops.cn\/index.php\/wp-json\/wp\/v2\/tags?post=12982"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}