{"id":494,"date":"2018-05-01T14:51:09","date_gmt":"2018-05-01T06:51:09","guid":{"rendered":"https:\/\/yyshell.com\/?p=494"},"modified":"2018-05-01T14:51:09","modified_gmt":"2018-05-01T06:51:09","slug":"python%e5%88%97%e8%a1%a8","status":"publish","type":"post","link":"https:\/\/yyshell.com\/?p=494","title":{"rendered":"Python\u5217\u8868"},"content":{"rendered":"<p>\u4e0a\u4e00\u7bc7\u6211\u4eec\u4ecb\u7ecd\u4e86Python\u4e2d\u7684\u53d8\u91cf\u548c\u7b80\u5355\u6570\u636e\u7c7b\u578b\uff0c\u672c\u7bc7\u6211\u4eec\u4e3b\u8981\u4ecb\u7ecd\u5217\u8868\u3002<\/p>\n<p>\u5217\u8868\u662f\u4e00\u7cfb\u5217\u6309\u7279\u5b9a\u987a\u5e8f\u6392\u5217\u7684\u5143\u7d20\u7ec4\u6210\uff0c\u5728Python\u4e2d\uff0c\u7528\u65b9\u62ec\u53f7\u6765\u8868\u793a\u5217\u8868\uff0c\u7528\u9017\u53f7\u6765\u5206\u9694\u5176\u4e2d\u7684\u5143\u7d20\u3002<\/p>\n<p>\u4f8b\u5982\u6211\u4eec\u5b9a\u4e49\u4e00\u4e2a\u5217\u8868companies<\/p>\n<pre class=\"lang:python decode:true\">companies = ['Google', 'Microsoft', 'Amazon', 'Facebook', 'Apple']\r\nprint(companies)<\/pre>\n<p>\u8f93\u51fa\u7ed3\u679c\uff1a<\/p>\n<pre class=\"lang:python decode:true \">['Google', 'Microsoft', 'Amazon', 'Facebook', 'Apple']<\/pre>\n<h3>\u8bbf\u95ee\u5217\u8868\u4e2d\u7684\u5143\u7d20<\/h3>\n<p>\u6bd4\u5982\u6211\u4eec\u8981\u8bbf\u95ee\u4e0a\u8ff0companies\u5217\u8868\u4e2d\u7684\u7b2c\u4e00\u4e2a\u5143\u7d20\uff0c\u6211\u4eec\u53ef\u4ee5\u8fd9\u6837\uff1a<\/p>\n<pre class=\"lang:python decode:true \">print(companies[0])<\/pre>\n<p>\u8f93\u51fa\u7ed3\u679cGoogle<\/p>\n<p>\u5373\u91c7\u7528\u5217\u8868\u540d\u52a0\u4e2d\u62ec\u53f7[\u7d22\u5f15\u53f7]\u7684\u5f62\u5f0f\u6765\u63d0\u53d6\u5143\u7d20\uff0c\u9700\u8981\u6ce8\u610f\u7684\u662f\uff0c\u548c\u8ba1\u7b97\u673a\u7684\u5176\u4ed6\u673a\u5236\u7c7b\u4f3c\uff0c\u7b2c\u4e00\u4e2a\u7d22\u5f15\u662f\u4ee50\u5f00\u59cb\u7684\uff0c\u7b2c\u4e8c\u4e2a\u5143\u7d20\u7684\u7d22\u5f15\u53f7\u662f1\uff0c\u7136\u540e\u4ee5\u6b64\u7c7b\u63a8\u3002<\/p>\n<p>\u7d22\u5f15\u53f7-1\u662f\u53ea\u5217\u8868\u7684\u6700\u540e\u4e00\u4e2a\u5143\u7d20\uff1a<\/p>\n<pre class=\"lang:python decode:true \">print(companies[-1])<\/pre>\n<p>\u8f93\u51fa\u7ed3\u679cApple<\/p>\n<p>\u7d22\u5f15-2\u5219\u662f\u5012\u6570\u7b2c\u4e8c\u4e2a\u5143\u7d20\uff0c\u7136\u540e\u4ee5\u6b64\u7c7b\u63a8\u3002<\/p>\n<h3>\u4fee\u6539\u3001\u6dfb\u52a0\u3001\u5220\u9664\u5143\u7d20<\/h3>\n<p>\u8981\u4fee\u6539\u5143\u7d20\uff0c\u53ef\u4ee5\u76f4\u63a5\u5bf9\u8fd9\u4e2a\u5143\u7d20\u7684\u7d22\u5f15\u53f7\u5bf9\u5e94\u7684\u5185\u5bb9\u8d4b\u503c\uff0c\u4f8b\u5982\uff0c\u6211\u4eec\u8981\u628a\u4e0a\u8ff0companies\u5217\u8868\u7684\u7b2c\u4e00\u4e2a\u7d22\u5f15\u516c\u53f8\u540d\u6539\u6210Tencent\uff0c\u53ef\u4ee5\u8fd9\u6837\uff1a<\/p>\n<pre class=\"lang:python decode:true\">companies[0] = 'Tencent'\r\nprint(companies)<\/pre>\n<p>\u8f93\u51fa\u7ed3\u679c\u5c31\u53d8\u6210\u4e86\u8fd9\u6837\uff1a<\/p>\n<pre class=\"lang:python decode:true \">['Tencent', 'Microsoft', 'Amazon', 'Facebook', 'Apple']<\/pre>\n<p>\u8981\u6dfb\u52a0\u5143\u7d20\uff0c\u6bd4\u5982\u5728\u672b\u5c3e\u8ffd\u52a0\u4e00\u4e2a\u516c\u53f8Twitter\uff0c\u5219\u53ef\u4ee5\u8fd9\u6837\uff1a<\/p>\n<pre class=\"lang:sh decode:true \">companies.append('Twitter')\r\nprint(companies)<\/pre>\n<p>\u8f93\u51fa\u7ed3\u679c\u53d8\u6210\u8fd9\u6837\uff1a<\/p>\n<pre class=\"lang:python decode:true\">['Tencent', 'Microsoft', 'Amazon', 'Facebook', 'Apple', 'Twitter']<\/pre>\n<p>\u8981\u5728\u5217\u8868\u4e2d\u63d2\u5165\u5143\u7d20\uff0c\u53ef\u4ee5\u76f4\u63a5\u628a\u5143\u7d20\u63d2\u5165\u540e\u7684\u53f7\u548c\u5185\u5bb9\u52a0\u5165\u5373\u53ef\uff0c\u4f8b\u5982\uff0c\u6211\u4eec\u60f3\u628a\u521a\u624d\u88ab\u4fee\u6539\u6389\u7684Google\u91cd\u65b0\u52a0\u5165\u5217\u8868\uff0c\u5e76\u6392\u5217\u8868\u7684\u7b2c\u4e00\u4f4d\uff0c\u6211\u4eec\u53ef\u4ee5\u8fd9\u6837\uff1a<\/p>\n<pre class=\"lang:python decode:true \">companies.insert(0, 'Google')\r\nprint(companies)<\/pre>\n<p>\u8f93\u51fa\u7ed3\u679c\u53d8\u6210\u4e86\u8fd9\u6837\uff1a<\/p>\n<pre class=\"lang:python decode:true \">['Google', 'Tencent', 'Microsoft', 'Amazon', 'Facebook', 'Apple', 'Twitter']<\/pre>\n<p>\u4ece\u5217\u8868\u4e2d\u5220\u9664\u5143\u7d20\uff0c\u5982\u679c\u77e5\u9053\u5143\u7d20\u5728\u5217\u8868\u4e2d\u7684\u5177\u4f53\u4f4d\u7f6e\uff0c\u53ef\u4ee5\u4f7f\u7528del\u8fdb\u884c\u5220\u9664\u3002<\/p>\n<pre class=\"lang:python decode:true\">del companies[1]\r\nprint(companies)<\/pre>\n<p>\u8f93\u51fa\u7ed3\u679c\uff1a<\/p>\n<pre class=\"lang:python decode:true \">['Google', 'Microsoft', 'Amazon', 'Facebook', 'Apple', 'Twitter']<\/pre>\n<p>\u6709\u65f6\u5019\uff0c\u4f60\u9700\u8981\u5220\u9664\u67d0\u4e2a\u5143\u7d20\uff0c\u5e76\u63a5\u7740\u7ee7\u7eed\u4f7f\u7528\u8fd9\u4e2a\u5220\u6389\u7684\u5143\u7d20\uff0c\u90a3\u4e48\u8fd9\u65f6\u5019\u53ef\u4ee5\u4f7f\u7528pop()\uff0c\u4f8b\u5982<\/p>\n<pre class=\"lang:python decode:true \">popped_company = companies.pop()\r\nprint(companies)\r\nprint(popped_company)<\/pre>\n<p>\u8f93\u51fa\u7ed3\u679c\uff1a<\/p>\n<pre class=\"lang:python decode:true \">['Google', 'Microsoft', 'Amazon', 'Facebook', 'Apple']\r\nTwitter<\/pre>\n<p>\u8981\u5f39\u51fa\u5217\u8868\u4e2d\u4efb\u610f\u4f4d\u7f6e\u7684\u5143\u7d20\uff0c\u9700\u8981\u5728pop()\u4e2d\u52a0\u5165\u7d22\u5f15\u53f7\uff0c\u4f8b\u5982\u8981\u5f39\u51fa\u7b2c\u4e00\u4e2a\u5143\u7d20\uff0c\u5219\u4f7f\u7528pop(0)\u3002<\/p>\n<p>\u6709\u65f6\u5019\u6211\u4eec\u4e0d\u77e5\u9053\u5143\u7d20\u7684\u7d22\u5f15\u53f7\uff0c\u6211\u4eec\u4e5f\u53ef\u4ee5\u6839\u636e\u503c\u5220\u9664\u5177\u4f53\u7684\u503c\u5220\u9664\u5143\u7d20\uff0c\u6b64\u65f6\u53ef\u4ee5\u4f7f\u7528\u65b9\u6cd5remove()\uff0c\u4f8b\u5982\u6211\u4eec\u8981\u5220\u9664\u4e0a\u8ff0\u516c\u53f8\u4e2d\u7684Facebook\u516c\u53f8\uff0c\u53ef\u4ee5\u8fd9\u6837\u505a\u3002<\/p>\n<pre class=\"lang:python decode:true \">companies.remove('Facebook')\r\nprint(companies)<\/pre>\n<p>\u8f93\u51fa\u7ed3\u679c\u5982\u4e0b\uff1a<\/p>\n<pre class=\"lang:python decode:true \">['Google', 'Microsoft', 'Amazon', 'Apple']<\/pre>\n<p>\u9700\u8981\u6ce8\u610f\u7684\u662f\uff0c\u7528\u65b9\u6cd5remove()\u5220\u9664\u7684\u503c\uff0c\u4e5f\u53ef\u4ee5\u7ee7\u7eed\u4f7f\u7528\uff0c\u53e6\u5916\uff0c\u65b9\u6cd5remove()\u53ea\u5220\u9664\u5217\u8868\u4e2d\u7b2c\u4e00\u4e2a\u5339\u914d\u7684\u503c\uff0c\u5982\u679c\u6709\u591a\u4e2a\u91cd\u590d\u7684\u503c\uff0c\u5219\u9700\u8981\u4f7f\u7528\u5faa\u73af\u6765\u5220\u9664\u6240\u6709\u8fd9\u6837\u7684\u503c\u3002<\/p>\n<h3>\u7ec4\u7ec7\u5217\u8868<\/h3>\n<p>\u4f7f\u7528\u65b9\u6cd5sort()\u5bf9\u5217\u8868\u8fdb\u884c\u6c38\u4e45\u6027\u6392\u5e8f\u3002<\/p>\n<pre class=\"lang:python decode:true\">companies.sort()\r\nprint(companies)<\/pre>\n<p>\u8f93\u51fa\u7ed3\u679c\u5982\u4e0b\uff1a<\/p>\n<pre class=\"lang:python decode:true \">['Amazon', 'Apple', 'Google', 'Microsoft']<\/pre>\n<p>\u5982\u679c\u8981\u6309\u7167\u76f8\u53cd\u5b57\u6bcd\u7684\u987a\u5e8f\uff0c\u53ef\u4ee5\u6dfb\u52a0\u53c2\u6570reverse=True<\/p>\n<pre class=\"lang:python decode:true \">companies.sort(reverse=True)\r\nprint(companies)<\/pre>\n<p>\u8f93\u51fa\u7ed3\u679c\uff1a<\/p>\n<pre class=\"lang:python decode:true \">['Microsoft', 'Google', 'Apple', 'Amazon']<\/pre>\n<p>\u5982\u679c\u8981\u5bf9\u5217\u8868\u8fdb\u884c\u4e34\u65f6\u6027\u6392\u5e8f\uff0c\u5219\u53ef\u4ee5\u4f7f\u7528\u51fd\u6570sorted()\uff0c\u4f8b\u5982\u4f7f\u7528sorted(companies)\u53ef\u4ee5\u8f93\u51fa\u4e34\u65f6\u6392\u5e8f\u7684\u5185\u5bb9\uff0c\u5982\u679c\u8981\u8f93\u51fa\u4e0e\u5b57\u6bcd\u76f8\u53cd\u7684\u4e34\u65f6\u6392\u5e8f\uff0c\u540c\u6837\u9700\u8981\u6dfb\u52a0\u53c2\u6570reverse = True\uff0c\u4f8b\u5982sorted(companies, reverse = True)<\/p>\n<p>\u8981\u5012\u7740\u6253\u5370\u5217\u8868\uff0c\u5219\u53ef\u4ee5\u4f7f\u7528\u65b9\u6cd5reverse()\uff0c\u4f8b\u5982companies.reverse()\uff0c\u4f7f\u7528\u65b9\u6cd5reverse()\u4f1a\u6c38\u4e45\u6027\u7684\u6539\u53d8\u5217\u8868\uff0c\u8981\u6539\u53d8\u4e3a\u539f\u6765\u7684\u987a\u5e8f\uff0c\u5219\u518d\u4f7f\u7528\u4e00\u6b21\u65b9\u6cd5reverse()\u5373\u53ef\u3002<\/p>\n<p>\u8981\u786e\u5b9a\u5217\u8868\u7684\u957f\u5ea6\uff08\u5143\u7d20\u4e2a\u6570\uff09\uff0c\u53ef\u4ee5\u4f7f\u7528\u51fd\u6570len()\uff0c\u4f8b\u5982len(companies)<\/p>\n<p>\u5728\u4f7f\u7528\u5217\u8868\u7684\u65f6\u5019\uff0c\u7ecf\u5e38\u4f1a\u9047\u5230\u7d22\u5f15\u9519\u8bef\uff0c\u9996\u5148\u6211\u4eec\u8981\u660e\u786e\uff0c\u7d22\u5f15\u662f\u4ece0\u5f00\u59cb\u7684\uff1b\u5176\u6b21\uff0c\u6700\u540e\u4e00\u4e2a\u5143\u7d20\u7684\u7d22\u5f15\u662f-1\uff0c\u67d0\u4e9b\u64cd\u4f5c\u4e0b\uff0c\u5217\u8868\u7684\u5185\u5bb9\u4f1a\u52a8\u6001\u53d8\u5316\uff0c\u4f60\u6240\u4f7f\u7528\u7684\u7d22\u5f15\u5e8f\u53f7\u53ef\u80fd\u4e0e\u4f60\u903e\u671f\u7684\u5143\u7d20\u622a\u7136\u4e0d\u540c\uff0c\u6b64\u65f6\uff0c\u5efa\u8bae\u4f60\u76f4\u63a5\u7528print\u8f93\u51fa\u5f53\u524d\u5217\u8868\u7684\u5185\u5bb9\u3002<\/p>\n<p>&nbsp;<\/p>\n<div id=\"icibaCirclePointer\" style=\"position: absolute; top: 1344px; left: -54px;\"><\/div>\n","protected":false},"excerpt":{"rendered":"<p>\u4e0a\u4e00\u7bc7\u6211\u4eec\u4ecb\u7ecd\u4e86Python\u4e2d\u7684\u53d8\u91cf\u548c\u7b80\u5355\u6570\u636e\u7c7b\u578b\uff0c\u672c\u7bc7\u6211\u4eec\u4e3b\u8981\u4ecb\u7ecd\u5217\u8868\u3002 \u5217\u8868<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[78],"tags":[],"class_list":["post-494","post","type-post","status-publish","format-standard","hentry","category-python"],"_links":{"self":[{"href":"https:\/\/yyshell.com\/index.php?rest_route=\/wp\/v2\/posts\/494","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/yyshell.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/yyshell.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/yyshell.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/yyshell.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=494"}],"version-history":[{"count":3,"href":"https:\/\/yyshell.com\/index.php?rest_route=\/wp\/v2\/posts\/494\/revisions"}],"predecessor-version":[{"id":522,"href":"https:\/\/yyshell.com\/index.php?rest_route=\/wp\/v2\/posts\/494\/revisions\/522"}],"wp:attachment":[{"href":"https:\/\/yyshell.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=494"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/yyshell.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=494"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/yyshell.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=494"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}