{"id":3464,"date":"2026-11-29T13:13:44","date_gmt":"2026-11-29T04:13:44","guid":{"rendered":"https:\/\/danlabs.kr\/?p=3464"},"modified":"2026-11-29T13:14:03","modified_gmt":"2026-11-29T04:14:03","slug":"python-groupby-adjacent-20261129","status":"publish","type":"post","link":"https:\/\/danlabs.kr\/?p=3464","title":{"rendered":"Python groupby\ub85c \ubb36\uae30: \uac19\uc740 \ud0a4\uac00 \ub450 \ubc88 \ub098\uc624\ub294 \uc774\uc720"},"content":{"rendered":"<p>\uc8fc\ubb38 \ud30c\uc77c\uc758 \ucc3d\uace0 \uc5f4\uc5d0 <code>A, B, A<\/code>\uac00 \ub4e4\uc5b4 \uc788\uc73c\uba74 Python\uc758 <code>itertools.groupby()<\/code>\ub294 A\ub97c \ub450 \ubc88 \ub3cc\ub824\uc918\uc694.<sup><a href=\"#source-1\">[1]<\/a><\/sup> \uac19\uc740 \uac12\uc774 \uc55e\uc5d0 \uc788\uc5c8\ub294\uc9c0 \uae30\uc5b5\ud574\uc11c \ud569\uce58\ub294 \ub300\uc2e0, \ubc14\ub85c \uc55e \ud589\uacfc \ud0a4\uac00 \ub2ec\ub77c\uc9c0\ub294 \uc790\ub9ac\uc5d0\uc11c \uadf8\ub8f9\uc744 \ub04a\uae30 \ub54c\ubb38\uc774\uc5d0\uc694.<sup><a href=\"#source-1\">[1]<\/a><\/sup> \ucc3d\uace0\ubcc4 \ud53c\ud0b9 \ubaa9\ub85d\uc5d0\uc120 A\ub97c \ud55c \ubc88\ub9cc \ubc1b\uace0 \uc2f6\uc9c0\ub9cc, \uc0c1\ud0dc \ub85c\uadf8\uc5d0\uc11c A\ub85c \ub3cc\uc544\uc628 \uc2dc\uc810\uc774 \uc911\uc694\ud558\ub2e4\uba74 \ub450 \uad6c\uac04\uc744 \uadf8\ub300\ub85c \ub0a8\uaca8\uc57c \ud574\uc694.<\/p>\n<figure><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/danlabs.kr\/wp-content\/uploads\/2026\/11\/python-groupby-adjacent-20261129-inline1.jpg\" alt=\"\uc785\ub825 \uc21c\uc11c\uc5d0\uc11c \uac00, \ub098, \uac00 \ucc3d\uace0\uac00 \uc138 \uc5f0\uc18d \uad6c\uac04\uc73c\ub85c \ub098\ub258\ub294 \uc0dd\uc131 \ub3c4\uc2dd\" width=\"1280\" height=\"720\" loading=\"lazy\"><\/figure>\n<h2>\ud0a4\uac00 \ubc14\ub014 \ub54c\ub9c8\ub2e4 \uc0c8 \uadf8\ub8f9\uc774 \uc5f4\ub824\uc694<\/h2>\n<p><code>groupby(records, key=warehouse)<\/code>\uc5d0 \ud589\uc5d0\uc11c \ucc3d\uace0 \uac12\uc744 \uaebc\ub0b4\ub294 \ud568\uc218\ub97c \ub118\uae30\uba74, \uadf8 \ud568\uc218\uac00 \uac19\uc740 \uac12\uc744 \ub3cc\ub824\uc8fc\ub294 \ub3d9\uc548\uc740 \ud55c \ubb36\uc74c\uc774\uc5d0\uc694.<sup><a href=\"#source-1\">[1]<\/a><\/sup> <code>key<\/code>\ub97c \uc0dd\ub7b5\ud558\uba74 \ud589 \uc804\uccb4\ub97c \uadf8\ub300\ub85c \ud0a4\ub85c \uc4f0\ubbc0\ub85c \ucc3d\uace0 \uac12\uc774 \uac19\uc544\ub3c4 SKU\uac00 \ub2e4\ub974\uba74 \uac19\uc740 \ubb36\uc74c\uc73c\ub85c \uc7a1\ud788\uc9c0 \uc54a\uc544\uc694.<sup><a href=\"#source-1\">[1]<\/a><\/sup><\/p>\n<p>SQL\uc758 <code>GROUP BY<\/code>\ub294 \uc785\ub825 \uc21c\uc11c\uc640 \uad00\uacc4\uc5c6\uc774 \uacf5\ud1b5 \ud0a4\ub97c \ubb36\uc9c0\ub9cc, <code>itertools.groupby()<\/code>\ub294 \uc785\ub825 \uc21c\uc11c\uac00 \uacbd\uacc4\ub97c \uacb0\uc815\ud574\uc694.<sup><a href=\"#source-1\">[1]<\/a><\/sup> \uc7a5\ube44 \uc0c1\ud0dc\uac00 <code>\ub300\uae30 \u2192 \uc791\uc5c5 \u2192 \ub300\uae30<\/code>\ub85c \ubc14\ub010 \ub85c\uadf8\ub77c\uba74 \uc55e\ub4a4\uc758 \ub300\uae30\ub97c \ubcc4\uac1c \uad6c\uac04\uc73c\ub85c \ub0a8\uae30\ub294 \ub370 \uc774 \ub3d9\uc791\uc774 \ub4e4\uc5b4\ub9de\uc544\uc694.<\/p>\n<h2>\ucc3d\uace0\ub9c8\ub2e4 \ud55c \ubb36\uc74c\uc774 \ud544\uc694\ud558\uba74 \uac19\uc740 \ud0a4\ub85c \uc815\ub82c\ud574\uc694<\/h2>\n<p>\ucc3d\uace0\ubcc4 \ubaa9\ub85d\uc5d0\uc11c\ub294 <code>sorted(rows, key=warehouse)<\/code>\ub85c \uac19\uc740 \ucc3d\uace0\uc758 \ud589\uc744 \ubd99\uc778 \ub4a4, \uac19\uc740 <code>warehouse<\/code> \ud568\uc218\ub85c \uadf8\ub8f9\uc744 \ub098\ub220\uc694.<sup><a href=\"#source-1\">[1]<\/a><\/sup><sup><a href=\"#source-2\">[2]<\/a><\/sup> <code>sorted()<\/code>\ub294 \uc0c8 \ub9ac\uc2a4\ud2b8\ub97c \ub9cc\ub4e4\uace0, \uc815\ub82c \ud0a4\uac00 \uac19\uc740 \ud589\ub07c\ub9ac\ub294 \uae30\uc874 \uc21c\uc11c\ub97c \uc9c0\ucf1c\uc694.<sup><a href=\"#source-2\">[2]<\/a><\/sup> \uadf8\ub798\uc11c \uc544\ub798 \uc608\uc2dc\uc758 A \ucc3d\uace0 \uc548\uc5d0\uc11c\ub294 <code>a-01<\/code>\uc774 \uc5ec\uc804\ud788 <code>a-03<\/code> \uc55e\uc5d0 \uc640\uc694.<\/p>\n<figure><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/danlabs.kr\/wp-content\/uploads\/2026\/11\/python-groupby-adjacent-20261129-inline2.jpg\" alt=\"\ucc3d\uace0 \ud0a4\ub85c \uc815\ub82c\ud574 \uac00 \ucc3d\uace0 \ud589\uc744 \ud558\ub098\uc758 \uce74\ub4dc\uc5d0 \ubaa8\uc740 \uc0dd\uc131 \ub3c4\uc2dd\" width=\"1280\" height=\"720\" loading=\"lazy\"><\/figure>\n<p>\uc544\ub798\ub294 Python 3.14.9\uc5d0\uc11c \uc6d0\ubcf8\uacfc \uc815\ub82c\ubcf8\uc758 \ubb36\uc74c\uc744 \ub098\ub780\ud788 \ucd9c\ub825\ud55c \uacb0\uacfc\uc608\uc694. \ub9c8\uc9c0\ub9c9 \uc904\uc740 \ub2e4\uc74c \uadf8\ub8f9\uc73c\ub85c \ub118\uc5b4\uac04 \ub4a4 \uccab \uadf8\ub8f9\uc744 \uc77d\uc740 \uac12\uc774\uc5d0\uc694.<\/p>\n<pre><code class=\"language-python\">from itertools import groupby\nfrom operator import itemgetter\n\nrows = [(\"A\", \"a-01\"), (\"B\", \"b-02\"), (\"A\", \"a-03\")]\nwarehouse = itemgetter(0)\n\n\ndef batches(records):\n    return [(key, [sku for _, sku in group])\n            for key, group in groupby(records, key=warehouse)]\n\n\nprint(\"input: \", batches(rows))\nprint(\"sorted:\", batches(sorted(rows, key=warehouse)))\n\nouter = groupby(rows, key=warehouse)\nkey, first = next(outer)\nnext(outer)\nprint(\"late:  \", (key, list(first)))<\/code><\/pre>\n<pre><code class=\"language-text\">input:  [('A', ['a-01']), ('B', ['b-02']), ('A', ['a-03'])]\nsorted: [('A', ['a-01', 'a-03']), ('B', ['b-02'])]\nlate:   ('A', [])<\/code><\/pre>\n<h2>\uadf8\ub8f9\uc744 \ub098\uc911\uc5d0 \uc4f0\ub824\uba74 \uc9c0\uae08 \uaebc\ub0b4 \ub46c\uc694<\/h2>\n<p><code>late<\/code>\uc758 \ube48 \ub9ac\uc2a4\ud2b8\ub294 \uccab A \ud589\uc774 \uc5c6\uc5c8\ub2e4\ub294 \ud45c\uc2dc\uac00 \uc544\ub2c8\uc5d0\uc694. \uadf8\ub8f9 \ubc18\ubcf5\uc790\uc640 \ubc14\uae65 <code>groupby()<\/code>\uac00 \uac19\uc740 \uc785\ub825\uc744 \uacf5\uc720\ud558\ubbc0\ub85c \ubc14\uae65\ucabd\uc744 \ub2e4\uc74c \uadf8\ub8f9\uc73c\ub85c \uc804\uc9c4\uc2dc\ud0a4\uba74 \uc774\uc804 \uadf8\ub8f9\uc744 \ub354\ub294 \uc77d\uc744 \uc218 \uc5c6\uc5b4\uc694.<sup><a href=\"#source-1\">[1]<\/a><\/sup> \uc608\uc81c\uc758 <code>batches()<\/code>\ucc98\ub7fc \ud544\uc694\ud55c \uac12\uc744 \uadf8 \uc790\ub9ac\uc5d0\uc11c \ub9ac\uc2a4\ud2b8\ub85c \uaebc\ub0b4\uba74, \ub2e4\uc74c \uadf8\ub8f9\uc73c\ub85c \ub118\uc5b4\uac04 \ub4a4\uc5d0\ub3c4 \uc800\uc7a5\ud55c \uac12\uc744 \uc4f8 \uc218 \uc788\uc5b4\uc694.<sup><a href=\"#source-1\">[1]<\/a><\/sup><\/p>\n<figure><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/danlabs.kr\/wp-content\/uploads\/2026\/11\/python-groupby-adjacent-20261129-inline3.jpg\" alt=\"\ud604\uc7ac \uadf8\ub8f9\uc5d0\uc11c \ud544\uc694\ud55c \uac12\uc744 \ub9ac\uc2a4\ud2b8\ub85c \uaebc\ub0b4 \uc800\uc7a5\ud558\ub294 \uc0dd\uc131 \ub3c4\uc2dd\" width=\"1280\" height=\"720\" loading=\"lazy\"><\/figure>\n<p>\uc785\ub825\uc774 \uc774\ubbf8 \ucc3d\uace0\ubcc4\ub85c \ubd99\uc5b4 \uc788\uace0 \uadf8\ub8f9\uc744 \ubc1b\ub294 \uc989\uc2dc \ucc98\ub9ac\ud55c\ub2e4\uba74 \uad73\uc774 <code>sorted()<\/code>\ub85c \uc804\uccb4 \ub9ac\uc2a4\ud2b8\ub97c \uc0c8\ub85c \ub9cc\ub4e4 \uc774\uc720\ub294 \uc5c6\uc5b4\uc694.<sup><a href=\"#source-1\">[1]<\/a><\/sup><sup><a href=\"#source-2\">[2]<\/a><\/sup> \ucc3d\uace0\ubcc4 \ubaa9\ub85d\uc774 \ud544\uc694\ud558\uc9c0\ub9cc \uadf8\ub8f9\ub3c4 \ucc3d\uace0 \uc774\ub984\uc21c\uc774 \uc544\ub2cc \uccab \ub4f1\uc7a5 \uc21c\uc11c\ub85c \ub0b4\ubcf4\ub0b4\uace0 \uc2f6\ub2e4\uba74, \uc785\ub825\uc744 \uc21c\uc11c\ub300\ub85c \uc77d\uc73c\uba74\uc11c \ub515\uc154\ub108\ub9ac\uc5d0 \ubaa8\uc73c\ub294 \ucabd\uc774 \uadf8 \uc694\uad6c\ub97c \ucf54\ub4dc\uc5d0 \uc9c1\uc811 \ub4dc\ub7ec\ub0b4\uc694.<\/p>\n<p>\ud30c\uc77c\uc758 \ud589\uc744 \uc774\ub984 \uc788\ub294 \uc5f4\ub85c \ubc1b\ub294 \ubc29\ubc95\uc740 <a href=\"https:\/\/danlabs.kr\/?p=3137\">Python csv.DictReader\uc5d0\uc11c \uc5f4 \ub204\ub77d\uacfc \ucd08\uacfc\ub97c \uad6c\ubd84\ud558\ub294 \uae00<\/a>\uc5d0 \uc774\uc5b4\uc838\uc694.<\/p>\n<p>\ub300\ud45c\uc640 \ubcf8\ubb38 \uadf8\ub9bc\uc740 \ucf54\ub4dc \ub3d9\uc791\uc744 \uc124\uba85\ud558\uae30 \uc704\ud55c \uc0dd\uc131 \uac1c\ub150 \uc774\ubbf8\uc9c0\uc608\uc694.<\/p>\n<p><strong>Sources<\/strong><\/p>\n<ol>\n<li id=\"source-1\"><a href=\"https:\/\/docs.python.org\/3.14\/library\/itertools.html\" target=\"_blank\" rel=\"noopener noreferrer\">Python 3.14 \uacf5\uc2dd \ubb38\uc11c: itertools.groupby (2026-11-29 \uc870\ud68c)<\/a><\/li>\n<li id=\"source-2\"><a href=\"https:\/\/docs.python.org\/3.14\/howto\/sorting.html\" target=\"_blank\" rel=\"noopener noreferrer\">Python 3.14 \uacf5\uc2dd \ubb38\uc11c: Sorting Techniques (2026-11-29 \uc870\ud68c)<\/a><\/li>\n<\/ol>\n","protected":false},"excerpt":{"rendered":"<p>Python itertools.groupby\uc5d0\uc11c \uac19\uc740 \ucc3d\uace0 \ud0a4\uac00 \ub5a8\uc5b4\uc838 \uc788\uc73c\uba74 \uc65c \ub450 \ubb36\uc74c\uc73c\ub85c \ub098\ub260\uae4c\uc694? \uac19\uc740 \ud0a4 \uc815\ub82c\uacfc \uadf8\ub8f9 \ubc18\ubcf5\uc790 \uc18c\uc9c4\uc744 \uc2e4\ud589\ud55c \ucf54\ub4dc\ub85c \uc124\uba85\ud574\uc694.<\/p>\n","protected":false},"author":1,"featured_media":3460,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[19],"tags":[],"class_list":["post-3464","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-dev"],"featured_image_urls":{"full":["https:\/\/danlabs.kr\/wp-content\/uploads\/2026\/11\/python-groupby-adjacent-20261129-feat.jpg",1280,720,false],"thumbnail":["https:\/\/danlabs.kr\/wp-content\/uploads\/2026\/11\/python-groupby-adjacent-20261129-feat-150x150.jpg",150,150,true],"medium":["https:\/\/danlabs.kr\/wp-content\/uploads\/2026\/11\/python-groupby-adjacent-20261129-feat-300x169.jpg",300,169,true],"medium_large":["https:\/\/danlabs.kr\/wp-content\/uploads\/2026\/11\/python-groupby-adjacent-20261129-feat-768x432.jpg",640,360,true],"large":["https:\/\/danlabs.kr\/wp-content\/uploads\/2026\/11\/python-groupby-adjacent-20261129-feat-1024x576.jpg",640,360,true],"1536x1536":["https:\/\/danlabs.kr\/wp-content\/uploads\/2026\/11\/python-groupby-adjacent-20261129-feat.jpg",1280,720,false],"2048x2048":["https:\/\/danlabs.kr\/wp-content\/uploads\/2026\/11\/python-groupby-adjacent-20261129-feat.jpg",1280,720,false],"morenews-large":["https:\/\/danlabs.kr\/wp-content\/uploads\/2026\/11\/python-groupby-adjacent-20261129-feat-825x575.jpg",825,575,true],"morenews-medium":["https:\/\/danlabs.kr\/wp-content\/uploads\/2026\/11\/python-groupby-adjacent-20261129-feat-590x410.jpg",590,410,true],"danlabs-list-small":["https:\/\/danlabs.kr\/wp-content\/uploads\/2026\/11\/python-groupby-adjacent-20261129-feat-366x190.jpg",366,190,true],"danlabs-list-retina":["https:\/\/danlabs.kr\/wp-content\/uploads\/2026\/11\/python-groupby-adjacent-20261129-feat.jpg",1280,720,false]},"author_info":{"display_name":"DanLABs","author_link":"https:\/\/danlabs.kr\/author\/hello-danlabs-kr"},"category_info":"<a href=\"https:\/\/danlabs.kr\/?cat=19\" rel=\"category\">\uac1c\ubc1c<\/a>","tag_info":"\uac1c\ubc1c","comment_count":"0","_links":{"self":[{"href":"https:\/\/danlabs.kr\/index.php?rest_route=\/wp\/v2\/posts\/3464","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/danlabs.kr\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/danlabs.kr\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/danlabs.kr\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/danlabs.kr\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=3464"}],"version-history":[{"count":1,"href":"https:\/\/danlabs.kr\/index.php?rest_route=\/wp\/v2\/posts\/3464\/revisions"}],"predecessor-version":[{"id":3465,"href":"https:\/\/danlabs.kr\/index.php?rest_route=\/wp\/v2\/posts\/3464\/revisions\/3465"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/danlabs.kr\/index.php?rest_route=\/wp\/v2\/media\/3460"}],"wp:attachment":[{"href":"https:\/\/danlabs.kr\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=3464"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/danlabs.kr\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=3464"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/danlabs.kr\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=3464"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}