{"id":70,"date":"2017-01-23T20:34:07","date_gmt":"2017-01-23T11:34:07","guid":{"rendered":"http:\/\/www.python.ambitious-engineer.com\/?p=70"},"modified":"2020-01-19T12:35:30","modified_gmt":"2020-01-19T03:35:30","slug":"set%e5%9e%8b%e3%81%ae%e5%9f%ba%e6%9c%ac","status":"publish","type":"post","link":"https:\/\/www.python.ambitious-engineer.com\/archives\/70","title":{"rendered":"set\u578b\u306e\u57fa\u672c"},"content":{"rendered":"<p>Python\u306b\u306fset\u578b\u3068\u3044\u3046\u96c6\u5408\u3092\u8868\u3059\u5909\u6570\u306e\u578b\u304c\u7528\u610f\u3055\u308c\u3066\u3044\u307e\u3059\u3002\u30ea\u30b9\u30c8\u3084\u30bf\u30d7\u30eb\u306f\u540c\u3058\u5024\u3092\u91cd\u8907\u3057\u3066\u8ffd\u52a0\u3059\u308b\u3053\u3068\u304c\u3067\u304d\u3001\u8ffd\u52a0\u3057\u305f\u969b\u306e\u9806\u5e8f\u3092\u4fdd\u6301\u3059\u308b\u3053\u3068\u304c\u3067\u304d\u307e\u3057\u305f\u3002\u4e00\u65b9\u3067set\u306f\u9806\u5e8f\u306f\u4fdd\u6301\u305b\u305a\u30e6\u30cb\u30fc\u30af\u306a\u5024\u3092\u6301\u3061\u307e\u3059\u3002<\/p>\n<h2>set\u306e\u521d\u671f\u5316\u3068\u6ce8\u610f\u70b9<\/h2>\n<h3>set\u306e\u521d\u671f\u5316<\/h3>\n<p>set\u578b\u30aa\u30d6\u30b8\u30a7\u30af\u30c8\u306e\u521d\u671f\u5316\u306f\u3001\u4e2d\u62ec\u5f27\u306e\u4e2d\u306b\u30ab\u30f3\u30de\u533a\u5207\u308a\u3067\u8981\u7d20\u3092\u5217\u6319\u3057\u307e\u3059\u3002<\/p>\n<pre class=\"brush: python; title: ; notranslate\" title=\"\">\r\n# set\u306e\u521d\u671f\u5316\r\ns = {'A', 'B', 'C'}\r\nprint(s) # {'C', 'B', 'A'}\r\n\r\n# \u521d\u671f\u5316\u6642\u306b\u91cd\u8907\u304c\u3042\u3063\u3066\u3082\u30fb\u30fb\u30fb\r\ns = {'A', 'B', 'C', 'A'}\r\nprint(s) # {'C', 'B', 'A'}\r\n\r\n<\/pre>\n<p>2\u756a\u76ee\u306e\u4f8b\u306e\u3088\u3046\u306b\u3001\u91cd\u8907\u3057\u305f\u5024\u3092\u30bb\u30c3\u30c8\u3057\u3066\u30821\u3064\u3068\u3057\u3066\u6271\u308f\u308c\u307e\u3059\u3002<\/p>\n<h3>set\u306e\u6ce8\u610f\u70b9 \u300cTypeError unhashable type\u300d<\/h3>\n<p>\u307e\u305f\u3001\u683c\u7d0d\u3059\u308b\u5024\u306f\u30cf\u30c3\u30b7\u30e5\u5316\u53ef\u80fd\u3067\u306a\u3051\u308c\u3070\u306a\u308a\u307e\u305b\u3093\u3002\u3053\u308c\u306f\u3001set\u306f\u8981\u7d20\u306e\u30e6\u30cb\u30fc\u30af\u3055\u3092\u5224\u5b9a\u3059\u308b\u969b\u306b\u30cf\u30c3\u30b7\u30e5\u5024\u3092\u5229\u7528\u3059\u308b\u305f\u3081\u3067\u3059\u3002\u4f8b\u3048\u3070\u30ea\u30b9\u30c8\u306f\u30cf\u30c3\u30b7\u30e5\u5316\u53ef\u80fd\u3067\u306f\u306a\u3044\u305f\u3081set\u306b\u633f\u5165\u3059\u308b\u3053\u3068\u306f\u3067\u304d\u307e\u305b\u3093\u3002TypeError unhashable type\u304c\u767a\u751f\u3057\u307e\u3059\u3002<\/p>\n<pre class=\"brush: python; title: ; notranslate\" title=\"\">\r\ns = {'A', [1, 2, 3]}\r\nTraceback (most recent call last):\r\n  File &quot;&lt;stdin&gt;&quot;, line 1, in &lt;module&gt;\r\nTypeError: unhashable type: 'list'\r\n<\/pre>\n<p>\u30cf\u30c3\u30b7\u30e5\u5316\u53ef\u80fd\u306a\u30bf\u30d7\u30eb\u3092\u5229\u7528\u3059\u308c\u3070\u89e3\u6c7a\u3057\u307e\u3059\u3002<\/p>\n<pre class=\"brush: python; title: ; notranslate\" title=\"\">\r\ns = {'A', (1, 2, 3)}\r\nprint(s) # {'A', (1, 2, 3)}\r\n<\/pre>\n<h3>\u30ea\u30b9\u30c8\u304b\u3089set\u578b\u5909\u6570\u3092\u751f\u6210\u3059\u308b<\/h3>\n<p>\u7d44\u8fbc\u307f\u306eset\u95a2\u6570\u3092\u4f7f\u7528\u3059\u308b\u3068\u3001\u30ea\u30b9\u30c8\u304b\u3089set\u578b\u5909\u6570\u3092\u751f\u6210\u3059\u308b\u3053\u3068\u304c\u3067\u304d\u307e\u3059\u3002<\/p>\n<pre class=\"brush: python; title: ; notranslate\" title=\"\">\r\nl = ['a', 'b', 'c', 'a'] # list\u3092\u751f\u6210\r\ns = set(l) # set\u95a2\u6570\u3092\u4f7f\u7528\r\nprint(s) # {'a', 'b', 'c'}\r\n<\/pre>\n<p>\u30e6\u30cb\u30fc\u30af\u306a\u8981\u7d20\u306eset\u306b\u306a\u3063\u3066\u3044\u308b\u3053\u3068\u304c\u308f\u304b\u308a\u307e\u3059\u3002<\/p>\n<h2>set\u306e\u7e70\u308a\u8fd4\u3057\u51e6\u7406<\/h2>\n<p>set\u578b\u5909\u6570\u306f\u30ea\u30b9\u30c8\u306a\u3069\u3068\u540c\u69d8\u306b\u7e70\u308a\u8fd4\u3057\u51e6\u7406\u304c\u53ef\u80fd\u3067\u3059\u3002<\/p>\n<div class=\"c_box intitle yellow_box\"><div class=\"box_title\"><span>set\u306efor\u6587<\/span><\/div>\nfor \u5909\u6570 in set\u578b\u30aa\u30d6\u30b8\u30a7\u30af\u30c8:<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;\u51e6\u7406<br \/>\n<\/div>\n<pre class=\"brush: python; title: ; notranslate\" title=\"\">\r\ndata_set = {'a', 'b', 'c'}\r\nfor s in data_set:\r\n    print(s) # \u96c6\u5408\u306e\u8981\u7d20\u304c\u51fa\u529b\u3055\u308c\u308b\r\n<\/pre>\n<p>\u5b9f\u884c\u7d50\u679c\u306fa,b,c\u304c\u51fa\u529b\u3055\u308c\u307e\u3059\u3002<\/p>\n<h2>set\u578b\u5909\u6570\u306e\u66f4\u65b0\u64cd\u4f5c<\/h2>\n<p>set\u578b\u5909\u6570\u306e\u66f4\u65b0\u3067\u3059\u304c\u3001\u8981\u7d20\u306e\u8ffd\u52a0\u306badd\u3001\u8981\u7d20\u306e\u524a\u9664\u306bremove\u3001discard\u3092\u4f7f\u3044\u307e\u3059\u3002\u9806\u5e8f\u306f\u6301\u305f\u306a\u3044\u305f\u3081\u3001list\u306e\u3088\u3046\u306b\u30a4\u30f3\u30c7\u30c3\u30af\u30b9\u3092\u6307\u5b9a\u3059\u308b\u65b9\u6cd5\u306f\u3042\u308a\u307e\u305b\u3093\u3002<\/p>\n<h3>set\u306e\u8981\u7d20\u8ffd\u52a0<\/h3>\n<p>\u8981\u7d20\u8ffd\u52a0\u306fadd\u30e1\u30bd\u30c3\u30c9\u3092\u4f7f\u3044\u307e\u3059\u3002\u4ee5\u4e0b\u306e\u69cb\u6587\u3068\u306a\u308a\u307e\u3059\u3002<br \/>\n<div class=\"c_box intitle yellow_box\"><div class=\"box_title\"><span>\u8981\u7d20\u8ffd\u52a0<\/span><\/div>set\u30aa\u30d6\u30b8\u30a7\u30af\u30c8.add(\u8ffd\u52a0\u3059\u308b\u30aa\u30d6\u30b8\u30a7\u30af\u30c8)<\/div>\n<pre class=\"brush: python; title: ; notranslate\" title=\"\">\r\ns = {1, 2, 3}\r\ns.add(4)\r\nprint(s) # {1, 2, 3, 4}\r\n<\/pre>\n<h3>set\u306e\u8981\u7d20\u524a\u9664<\/h3>\n<p>\u8981\u7d20\u524a\u9664\u306fremove\u3068discard\u306e2\u3064\u304c\u7528\u610f\u3055\u308c\u3066\u3044\u307e\u3059\u3002remove\u306f\u5b58\u5728\u3057\u306a\u3044\u30aa\u30d6\u30b8\u30a7\u30af\u30c8\u3092\u6307\u5b9a\u3059\u308b\u3068KeyError\u304c\u767a\u751f\u3057\u307e\u3059\u3002<br \/>\n<div class=\"c_box intitle yellow_box\"><div class=\"box_title\"><span>remove<\/span><\/div>set\u30aa\u30d6\u30b8\u30a7\u30af\u30c8.remove(\u524a\u9664\u3059\u308b\u30aa\u30d6\u30b8\u30a7\u30af\u30c8)<\/div>\n<p>\u4e00\u65b9\u3001discard\u306f\u5b58\u5728\u3057\u306a\u3044\u30aa\u30d6\u30b8\u30a7\u30af\u30c8\u3092\u6307\u5b9a\u3057\u3066\u3082\u4f55\u3082\u8d77\u3053\u308a\u307e\u305b\u3093\u3002<br \/>\n<div class=\"c_box intitle yellow_box\"><div class=\"box_title\"><span>discard<\/span><\/div>set\u30aa\u30d6\u30b8\u30a7\u30af\u30c8.discard(\u524a\u9664\u3059\u308b\u30aa\u30d6\u30b8\u30a7\u30af\u30c8)<\/div>\n<p>\u30b5\u30f3\u30d7\u30eb\u3067\u78ba\u8a8d\u3057\u3066\u307f\u307e\u3057\u3087\u3046\u3002<\/p>\n<pre class=\"brush: python; title: ; notranslate\" title=\"\">\r\ns = {1, 2, 3, 4}\r\ns.remove(4)\r\nprint(s) # {1, 2, 3}\r\ns.remove(99) # KeyError\u304c\u767a\u751f\u3059\u308b\r\n\r\ns.discard(99)\r\nprint(s) # {1, 2, 3}\r\n<\/pre>\n<p>99\u3092\u524a\u9664\u3059\u308b\u969b\u3001remove\u3067\u306f\u30a8\u30e9\u30fc\u304c\u767a\u751f\u3057\u307e\u3059\u304c\u3001discard\u306e\u5834\u5408\u306f\u30a8\u30e9\u30fc\u304c\u767a\u751f\u3057\u3066\u3044\u306a\u3044\u3053\u3068\u304c\u78ba\u8a8d\u3067\u304d\u307e\u3059\u3002<\/p>\n<h2>frozenset \u30a4\u30df\u30e5\u30fc\u30bf\u30d6\u30eb\u306aset<\/h2>\n<p>\u6700\u5f8c\u306bset\u578b\u306e\u5144\u5f1f\u3067\u3042\u308bfrozenset\u306b\u3064\u3044\u3066\u7d39\u4ecb\u3057\u307e\u3059\u3002\u521d\u671f\u5316\u306e\u5f8c\u5909\u66f4\u3055\u305b\u306a\u3044\u3088\u3046\u306aset\u3092\u4f5c\u308a\u305f\u3044\u5834\u5408\u306ffrozenset\u578b\u3092\u4f7f\u7528\u3057\u307e\u3059\u3002\u30aa\u30d6\u30b8\u30a7\u30af\u30c8\u751f\u6210\u6642\u306b\u5f15\u6570\u306blist\u306e\u3088\u3046\u306a\u30a4\u30c6\u30e9\u30d6\u30eb\u306a\u30aa\u30d6\u30b8\u30a7\u30af\u30c8\u3092\u6307\u5b9a\u3057\u307e\u3059\u3002<\/p>\n<pre class=\"brush: python; title: ; notranslate\" title=\"\">\r\nfs = frozenset(['a', 'b', 'c'])\r\n<\/pre>\n<p>\u5909\u66f4\u4e0d\u53ef\u3068\u3044\u3046\u70b9\u3092\u9664\u304d\u3001\u307b\u3068\u3093\u3069\u306e\u64cd\u4f5c\u306fset\u3068\u304a\u306a\u3058\u3067\u3042\u308b\u305f\u3081\u3001\u7d30\u304b\u3044\u8aac\u660e\u306f\u5272\u611b\u3057\u307e\u3059\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Python\u306b\u306fset\u578b\u3068\u3044\u3046\u96c6\u5408\u3092\u8868\u3059\u5909\u6570\u306e\u578b\u304c\u7528\u610f\u3055\u308c\u3066\u3044\u307e\u3059\u3002\u30ea\u30b9\u30c8\u3084\u30bf\u30d7\u30eb\u306f\u540c\u3058\u5024\u3092\u91cd\u8907\u3057\u3066\u8ffd\u52a0\u3059\u308b\u3053\u3068\u304c\u3067\u304d\u3001\u8ffd\u52a0\u3057\u305f\u969b\u306e\u9806\u5e8f\u3092\u4fdd\u6301\u3059\u308b\u3053\u3068\u304c\u3067\u304d\u307e\u3057\u305f\u3002\u4e00\u65b9\u3067set\u306f\u9806\u5e8f\u306f\u4fdd\u6301\u305b\u305a\u30e6\u30cb\u30fc\u30af\u306a\u5024\u3092\u6301\u3061\u307e\u3059\u3002 s...<\/p>\n","protected":false},"author":1,"featured_media":3413,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[2],"tags":[48],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.python.ambitious-engineer.com\/wp-json\/wp\/v2\/posts\/70"}],"collection":[{"href":"https:\/\/www.python.ambitious-engineer.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.python.ambitious-engineer.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.python.ambitious-engineer.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.python.ambitious-engineer.com\/wp-json\/wp\/v2\/comments?post=70"}],"version-history":[{"count":11,"href":"https:\/\/www.python.ambitious-engineer.com\/wp-json\/wp\/v2\/posts\/70\/revisions"}],"predecessor-version":[{"id":177,"href":"https:\/\/www.python.ambitious-engineer.com\/wp-json\/wp\/v2\/posts\/70\/revisions\/177"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.python.ambitious-engineer.com\/wp-json\/wp\/v2\/media\/3413"}],"wp:attachment":[{"href":"https:\/\/www.python.ambitious-engineer.com\/wp-json\/wp\/v2\/media?parent=70"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.python.ambitious-engineer.com\/wp-json\/wp\/v2\/categories?post=70"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.python.ambitious-engineer.com\/wp-json\/wp\/v2\/tags?post=70"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}