https:\/\/docs.pythontab.com\/interpy\/set_data_structure\/set_data_structure\/<\/a> <\/p>\n\n\n\nset<\/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\nsome_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\nsome_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":"\n
5.0 set \u6570\u636e\u7ed3\u6784 - Linux\u81ea\u52a8\u5316\u8fd0\u7ef4<\/title>\n \n \n \n \n \n \n \n \n \n \n \n \n