{"id":515,"date":"2019-08-17T10:02:39","date_gmt":"2019-08-17T02:02:39","guid":{"rendered":"https:\/\/www.linuxdevops.cn\/?p=515"},"modified":"2019-10-15T16:17:14","modified_gmt":"2019-10-15T08:17:14","slug":"5ssjjg","status":"publish","type":"post","link":"https:\/\/www.linuxdevops.cn\/2019\/08\/5ssjjg\/","title":{"rendered":"5.0 set \u6570\u636e\u7ed3\u6784"},"content":{"rendered":"\n

set<\/code>(\u96c6\u5408)\u6570\u636e\u7ed3\u6784<\/h1>\n\n\n\n

\u539f\u6587\uff1a https:\/\/docs.pythontab.com\/interpy\/set_data_structure\/set_data_structure\/<\/a> <\/p>\n\n\n\n

set<\/code>(\u96c6\u5408)\u662f\u4e00\u4e2a\u975e\u5e38\u6709\u7528\u7684\u6570\u636e\u7ed3\u6784\u3002\u5b83\u4e0e\u5217\u8868(list<\/code>)\u7684\u884c\u4e3a\u7c7b\u4f3c\uff0c\u533a\u522b\u5728\u4e8eset<\/code>\u4e0d\u80fd\u5305\u542b\u91cd\u590d\u7684\u503c\u3002
\u8fd9\u5728\u5f88\u591a\u60c5\u51b5\u4e0b\u975e\u5e38\u6709\u7528\u3002\u4f8b\u5982\u4f60\u53ef\u80fd\u60f3\u68c0\u67e5\u5217\u8868\u4e2d\u662f\u5426\u5305\u542b\u91cd\u590d\u7684\u5143\u7d20\uff0c\u4f60\u6709\u4e24\u4e2a\u9009\u62e9\uff0c\u7b2c\u4e00\u4e2a\u9700\u8981\u4f7f\u7528for<\/code>\u5faa\u73af\uff0c\u5c31\u50cf\u8fd9\u6837\uff1a<\/p>\n\n\n\n

some_list = ['a', 'b', 'c', 'b', 'd', 'm', 'n', 'n']\n\nduplicates = []\nfor value in some_list:\n    if some_list.count(value) > 1:\n        if value not in duplicates:\n            duplicates.append(value)\n\nprint(duplicates)\n### \u8f93\u51fa: ['b', 'n']\n<\/code><\/pre>\n\n\n\n

\u4f46\u8fd8\u6709\u4e00\u79cd\u66f4\u7b80\u5355\u66f4\u4f18\u96c5\u7684\u89e3\u51b3\u65b9\u6848\uff0c\u90a3\u5c31\u662f\u4f7f\u7528\u96c6\u5408(sets)<\/code>\uff0c\u4f60\u76f4\u63a5\u8fd9\u6837\u505a\uff1a<\/p>\n\n\n\n

some_list = ['a', 'b', 'c', 'b', 'd', 'm', 'n', 'n']\nduplicates = set([x for x in some_list if some_list.count(x) > 1])\nprint(duplicates)\n### \u8f93\u51fa: set(['b', 'n'])\n<\/code><\/pre>\n\n\n\n

\u96c6\u5408\u8fd8\u6709\u4e00\u4e9b\u5176\u5b83\u65b9\u6cd5\uff0c\u4e0b\u9762\u6211\u4eec\u4ecb\u7ecd\u5176\u4e2d\u4e00\u90e8\u5206\u3002<\/p>\n\n\n\n

\u4ea4\u96c6<\/h2>\n\n\n\n

\u4f60\u53ef\u4ee5\u5bf9\u6bd4\u4e24\u4e2a\u96c6\u5408\u7684\u4ea4\u96c6\uff08\u4e24\u4e2a\u96c6\u5408\u4e2d\u90fd\u6709\u7684\u6570\u636e\uff09\uff0c\u5982\u4e0b\uff1a<\/p>\n\n\n\n

valid = set(['yellow', 'red', 'blue', 'green', 'black'])\ninput_set = set(['red', 'brown'])\nprint(input_set.intersection(valid))\n### \u8f93\u51fa: set(['red'])\n<\/code><\/pre>\n\n\n\n

\u5dee\u96c6<\/h2>\n\n\n\n

\u4f60\u53ef\u4ee5\u7528\u5dee\u96c6(difference)\u627e\u51fa\u65e0\u6548\u7684\u6570\u636e\uff0c\u76f8\u5f53\u4e8e\u7528\u4e00\u4e2a\u96c6\u5408\u51cf\u53bb\u53e6\u4e00\u4e2a\u96c6\u5408\u7684\u6570\u636e\uff0c\u4f8b\u5982\uff1a<\/p>\n\n\n\n

valid = set(['yellow', 'red', 'blue', 'green', 'black'])\ninput_set = set(['red', 'brown'])\nprint(input_set.difference(valid))\n### \u8f93\u51fa: set(['brown'])\n<\/code><\/pre>\n\n\n\n

\u4f60\u4e5f\u53ef\u4ee5\u7528\u7b26\u53f7\u6765\u521b\u5efa\u96c6\u5408\uff0c\u5982\uff1a<\/p>\n\n\n\n

a_set = {'red', 'blue', 'green'}\nprint(type(a_set))\n### \u8f93\u51fa: <type 'set'>\n<\/code><\/pre>\n\n\n\n

\u96c6\u5408\u8fd8\u6709\u4e00\u4e9b\u5176\u5b83\u65b9\u6cd5\uff0c\u6211\u4f1a\u5efa\u8bae\u8bbf\u95ee\u5b98\u65b9\u6587\u6863\u5e76\u505a\u4e2a\u5feb\u901f\u9605\u8bfb\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"

set(\u96c6\u5408)\u6570\u636e\u7ed3\u6784 \u539f\u6587\uff1a https:\/\/docs.pythontab.com\/interpy\/set_<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[17,13],"tags":[],"yoast_head":"\n5.0 set \u6570\u636e\u7ed3\u6784 - Linux\u81ea\u52a8\u5316\u8fd0\u7ef4<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.linuxdevops.cn\/2019\/08\/5ssjjg\/\" \/>\n<meta property=\"og:locale\" content=\"zh_CN\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"5.0 set \u6570\u636e\u7ed3\u6784 - Linux\u81ea\u52a8\u5316\u8fd0\u7ef4\" \/>\n<meta property=\"og:description\" content=\"set(\u96c6\u5408)\u6570\u636e\u7ed3\u6784 \u539f\u6587\uff1a https:\/\/docs.pythontab.com\/interpy\/set_\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.linuxdevops.cn\/2019\/08\/5ssjjg\/\" \/>\n<meta property=\"og:site_name\" content=\"Linux\u81ea\u52a8\u5316\u8fd0\u7ef4\" \/>\n<meta property=\"article:published_time\" content=\"2019-08-17T02:02:39+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2019-10-15T08:17:14+00:00\" \/>\n<meta name=\"author\" content=\"\u7ba1\u7406\u5458\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/www.linuxdevops.cn\/2019\/08\/5ssjjg\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/www.linuxdevops.cn\/2019\/08\/5ssjjg\/\"},\"author\":{\"name\":\"\u7ba1\u7406\u5458\",\"@id\":\"https:\/\/www.linuxdevops.cn\/#\/schema\/person\/3e206335d5796fdd8679e449df72a0d1\"},\"headline\":\"5.0 set \u6570\u636e\u7ed3\u6784\",\"datePublished\":\"2019-08-17T02:02:39+00:00\",\"dateModified\":\"2019-10-15T08:17:14+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/www.linuxdevops.cn\/2019\/08\/5ssjjg\/\"},\"wordCount\":13,\"commentCount\":0,\"publisher\":{\"@id\":\"https:\/\/www.linuxdevops.cn\/#\/schema\/person\/3e206335d5796fdd8679e449df72a0d1\"},\"articleSection\":[\"Linux\",\"Python\"],\"inLanguage\":\"zh-CN\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\/\/www.linuxdevops.cn\/2019\/08\/5ssjjg\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.linuxdevops.cn\/2019\/08\/5ssjjg\/\",\"url\":\"https:\/\/www.linuxdevops.cn\/2019\/08\/5ssjjg\/\",\"name\":\"5.0 set \u6570\u636e\u7ed3\u6784 - Linux\u81ea\u52a8\u5316\u8fd0\u7ef4\",\"isPartOf\":{\"@id\":\"https:\/\/www.linuxdevops.cn\/#website\"},\"datePublished\":\"2019-08-17T02:02:39+00:00\",\"dateModified\":\"2019-10-15T08:17:14+00:00\",\"breadcrumb\":{\"@id\":\"https:\/\/www.linuxdevops.cn\/2019\/08\/5ssjjg\/#breadcrumb\"},\"inLanguage\":\"zh-CN\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.linuxdevops.cn\/2019\/08\/5ssjjg\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.linuxdevops.cn\/2019\/08\/5ssjjg\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"\u9996\u9875\",\"item\":\"https:\/\/www.linuxdevops.cn\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Linux\",\"item\":\"https:\/\/www.linuxdevops.cn\/linux\/\"},{\"@type\":\"ListItem\",\"position\":3,\"name\":\"5.0 set \u6570\u636e\u7ed3\u6784\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/www.linuxdevops.cn\/#website\",\"url\":\"https:\/\/www.linuxdevops.cn\/\",\"name\":\"Linux\u81ea\u52a8\u5316\u8fd0\u7ef4\",\"description\":\"Linux\u81ea\u52a8\u5316\u8fd0\u7ef4\u7b14\u8bb0\",\"publisher\":{\"@id\":\"https:\/\/www.linuxdevops.cn\/#\/schema\/person\/3e206335d5796fdd8679e449df72a0d1\"},\"alternateName\":\"linuxdevops\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/www.linuxdevops.cn\/?s={search_term_string}\"},\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"zh-CN\"},{\"@type\":[\"Person\",\"Organization\"],\"@id\":\"https:\/\/www.linuxdevops.cn\/#\/schema\/person\/3e206335d5796fdd8679e449df72a0d1\",\"name\":\"\u7ba1\u7406\u5458\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"zh-CN\",\"@id\":\"https:\/\/www.linuxdevops.cn\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/www.linuxdevops.cn\/wp-content\/uploads\/2019\/07\/cropped-index.jpg\",\"contentUrl\":\"https:\/\/www.linuxdevops.cn\/wp-content\/uploads\/2019\/07\/cropped-index.jpg\",\"width\":512,\"height\":512,\"caption\":\"\u7ba1\u7406\u5458\"},\"logo\":{\"@id\":\"https:\/\/www.linuxdevops.cn\/#\/schema\/person\/image\/\"},\"description\":\"\u7ba1\u7406\u5458\",\"url\":\"https:\/\/www.linuxdevops.cn\/author\/root\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"5.0 set \u6570\u636e\u7ed3\u6784 - Linux\u81ea\u52a8\u5316\u8fd0\u7ef4","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.linuxdevops.cn\/2019\/08\/5ssjjg\/","og_locale":"zh_CN","og_type":"article","og_title":"5.0 set \u6570\u636e\u7ed3\u6784 - Linux\u81ea\u52a8\u5316\u8fd0\u7ef4","og_description":"set(\u96c6\u5408)\u6570\u636e\u7ed3\u6784 \u539f\u6587\uff1a https:\/\/docs.pythontab.com\/interpy\/set_","og_url":"https:\/\/www.linuxdevops.cn\/2019\/08\/5ssjjg\/","og_site_name":"Linux\u81ea\u52a8\u5316\u8fd0\u7ef4","article_published_time":"2019-08-17T02:02:39+00:00","article_modified_time":"2019-10-15T08:17:14+00:00","author":"\u7ba1\u7406\u5458","twitter_card":"summary_large_image","schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/www.linuxdevops.cn\/2019\/08\/5ssjjg\/#article","isPartOf":{"@id":"https:\/\/www.linuxdevops.cn\/2019\/08\/5ssjjg\/"},"author":{"name":"\u7ba1\u7406\u5458","@id":"https:\/\/www.linuxdevops.cn\/#\/schema\/person\/3e206335d5796fdd8679e449df72a0d1"},"headline":"5.0 set \u6570\u636e\u7ed3\u6784","datePublished":"2019-08-17T02:02:39+00:00","dateModified":"2019-10-15T08:17:14+00:00","mainEntityOfPage":{"@id":"https:\/\/www.linuxdevops.cn\/2019\/08\/5ssjjg\/"},"wordCount":13,"commentCount":0,"publisher":{"@id":"https:\/\/www.linuxdevops.cn\/#\/schema\/person\/3e206335d5796fdd8679e449df72a0d1"},"articleSection":["Linux","Python"],"inLanguage":"zh-CN","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/www.linuxdevops.cn\/2019\/08\/5ssjjg\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/www.linuxdevops.cn\/2019\/08\/5ssjjg\/","url":"https:\/\/www.linuxdevops.cn\/2019\/08\/5ssjjg\/","name":"5.0 set \u6570\u636e\u7ed3\u6784 - Linux\u81ea\u52a8\u5316\u8fd0\u7ef4","isPartOf":{"@id":"https:\/\/www.linuxdevops.cn\/#website"},"datePublished":"2019-08-17T02:02:39+00:00","dateModified":"2019-10-15T08:17:14+00:00","breadcrumb":{"@id":"https:\/\/www.linuxdevops.cn\/2019\/08\/5ssjjg\/#breadcrumb"},"inLanguage":"zh-CN","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.linuxdevops.cn\/2019\/08\/5ssjjg\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.linuxdevops.cn\/2019\/08\/5ssjjg\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"\u9996\u9875","item":"https:\/\/www.linuxdevops.cn\/"},{"@type":"ListItem","position":2,"name":"Linux","item":"https:\/\/www.linuxdevops.cn\/linux\/"},{"@type":"ListItem","position":3,"name":"5.0 set \u6570\u636e\u7ed3\u6784"}]},{"@type":"WebSite","@id":"https:\/\/www.linuxdevops.cn\/#website","url":"https:\/\/www.linuxdevops.cn\/","name":"Linux\u81ea\u52a8\u5316\u8fd0\u7ef4","description":"Linux\u81ea\u52a8\u5316\u8fd0\u7ef4\u7b14\u8bb0","publisher":{"@id":"https:\/\/www.linuxdevops.cn\/#\/schema\/person\/3e206335d5796fdd8679e449df72a0d1"},"alternateName":"linuxdevops","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.linuxdevops.cn\/?s={search_term_string}"},"query-input":"required name=search_term_string"}],"inLanguage":"zh-CN"},{"@type":["Person","Organization"],"@id":"https:\/\/www.linuxdevops.cn\/#\/schema\/person\/3e206335d5796fdd8679e449df72a0d1","name":"\u7ba1\u7406\u5458","image":{"@type":"ImageObject","inLanguage":"zh-CN","@id":"https:\/\/www.linuxdevops.cn\/#\/schema\/person\/image\/","url":"https:\/\/www.linuxdevops.cn\/wp-content\/uploads\/2019\/07\/cropped-index.jpg","contentUrl":"https:\/\/www.linuxdevops.cn\/wp-content\/uploads\/2019\/07\/cropped-index.jpg","width":512,"height":512,"caption":"\u7ba1\u7406\u5458"},"logo":{"@id":"https:\/\/www.linuxdevops.cn\/#\/schema\/person\/image\/"},"description":"\u7ba1\u7406\u5458","url":"https:\/\/www.linuxdevops.cn\/author\/root\/"}]}},"_links":{"self":[{"href":"https:\/\/www.linuxdevops.cn\/wp-json\/wp\/v2\/posts\/515"}],"collection":[{"href":"https:\/\/www.linuxdevops.cn\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.linuxdevops.cn\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.linuxdevops.cn\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.linuxdevops.cn\/wp-json\/wp\/v2\/comments?post=515"}],"version-history":[{"count":1,"href":"https:\/\/www.linuxdevops.cn\/wp-json\/wp\/v2\/posts\/515\/revisions"}],"predecessor-version":[{"id":516,"href":"https:\/\/www.linuxdevops.cn\/wp-json\/wp\/v2\/posts\/515\/revisions\/516"}],"wp:attachment":[{"href":"https:\/\/www.linuxdevops.cn\/wp-json\/wp\/v2\/media?parent=515"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.linuxdevops.cn\/wp-json\/wp\/v2\/categories?post=515"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.linuxdevops.cn\/wp-json\/wp\/v2\/tags?post=515"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}