Skip to content

Commit abcab6b

Browse files
committed
fixes #155
1 parent 8ccb007 commit abcab6b

File tree

4 files changed

+60
-9
lines changed

4 files changed

+60
-9
lines changed

fastcore/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
__version__ = "1.2.5"
1+
__version__ = "1.2.6"

fastcore/basics.py

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -350,10 +350,14 @@ def range_of(x):
350350
return list(range(len(x)))
351351

352352
# Cell
353-
def groupby(x, key):
354-
"Like `itertools.groupby` but doesn't need to be sorted, and isn't lazy"
353+
def groupby(x, key, val=noop):
354+
"Like `itertools.groupby` but doesn't need to be sorted, and isn't lazy, plus some extensions"
355+
if isinstance(key,int): key = itemgetter(key)
356+
elif isinstance(key,str): key = attrgetter(key)
357+
if isinstance(val,int): val = itemgetter(val)
358+
elif isinstance(val,str): val = attrgetter(val)
355359
res = {}
356-
for o in x: res.setdefault(key(o), []).append(o)
360+
for o in x: res.setdefault(key(o), []).append(val(o))
357361
return res
358362

359363
# Cell

nbs/01_basics.ipynb

Lines changed: 51 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -222,7 +222,22 @@
222222
"cell_type": "code",
223223
"execution_count": null,
224224
"metadata": {},
225-
"outputs": [],
225+
"outputs": [
226+
{
227+
"data": {
228+
"text/plain": [
229+
"[('hi', ['hi']),\n",
230+
" (array(1), [array(1)]),\n",
231+
" (1, [1]),\n",
232+
" ([1, 2], [1, 2]),\n",
233+
" (range(0, 3), [0, 1, 2])]"
234+
]
235+
},
236+
"execution_count": null,
237+
"metadata": {},
238+
"output_type": "execute_result"
239+
}
240+
],
226241
"source": [
227242
"[(o,listify(o)) for o in\n",
228243
" ('hi',array(1),1, [1,2], range(3))]"
@@ -1978,10 +1993,14 @@
19781993
"outputs": [],
19791994
"source": [
19801995
"#export\n",
1981-
"def groupby(x, key):\n",
1982-
" \"Like `itertools.groupby` but doesn't need to be sorted, and isn't lazy\"\n",
1996+
"def groupby(x, key, val=noop):\n",
1997+
" \"Like `itertools.groupby` but doesn't need to be sorted, and isn't lazy, plus some extensions\"\n",
1998+
" if isinstance(key,int): key = itemgetter(key)\n",
1999+
" elif isinstance(key,str): key = attrgetter(key)\n",
2000+
" if isinstance(val,int): val = itemgetter(val)\n",
2001+
" elif isinstance(val,str): val = attrgetter(val)\n",
19832002
" res = {}\n",
1984-
" for o in x: res.setdefault(key(o), []).append(o)\n",
2003+
" for o in x: res.setdefault(key(o), []).append(val(o))\n",
19852004
" return res"
19862005
]
19872006
},
@@ -1994,6 +2013,34 @@
19942013
"test_eq(groupby('aa ab bb'.split(), itemgetter(0)), {'a':['aa','ab'], 'b':['bb']})"
19952014
]
19962015
},
2016+
{
2017+
"cell_type": "markdown",
2018+
"metadata": {},
2019+
"source": [
2020+
"Here's an example of how to *invert* a grouping, using an `int` as `key` (which uses `itemgetter`; passing a `str` will use `attrgetter`), and using a `val` function:"
2021+
]
2022+
},
2023+
{
2024+
"cell_type": "code",
2025+
"execution_count": null,
2026+
"metadata": {},
2027+
"outputs": [
2028+
{
2029+
"data": {
2030+
"text/plain": [
2031+
"{1: [0], 3: [0, 2], 7: [0], 5: [3, 7], 8: [4], 4: [5]}"
2032+
]
2033+
},
2034+
"execution_count": null,
2035+
"metadata": {},
2036+
"output_type": "execute_result"
2037+
}
2038+
],
2039+
"source": [
2040+
"d = {0: [1, 3, 7], 2: [3], 3: [5], 4: [8], 5: [4], 7: [5]}\n",
2041+
"groupby(((o,k) for k,v in d.items() for o in v), 0, 1)"
2042+
]
2043+
},
19972044
{
19982045
"cell_type": "code",
19992046
"execution_count": null,

settings.ini

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ author = Jeremy Howard and Sylvain Gugger
77
author_email = [email protected]
88
copyright = fast.ai
99
branch = master
10-
version = 1.2.5
10+
version = 1.2.6
1111
min_python = 3.6
1212
audience = Developers
1313
language = English

0 commit comments

Comments
 (0)