|
222 | 222 | "cell_type": "code", |
223 | 223 | "execution_count": null, |
224 | 224 | "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 | + ], |
226 | 241 | "source": [ |
227 | 242 | "[(o,listify(o)) for o in\n", |
228 | 243 | " ('hi',array(1),1, [1,2], range(3))]" |
|
1978 | 1993 | "outputs": [], |
1979 | 1994 | "source": [ |
1980 | 1995 | "#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", |
1983 | 2002 | " 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", |
1985 | 2004 | " return res" |
1986 | 2005 | ] |
1987 | 2006 | }, |
|
1994 | 2013 | "test_eq(groupby('aa ab bb'.split(), itemgetter(0)), {'a':['aa','ab'], 'b':['bb']})" |
1995 | 2014 | ] |
1996 | 2015 | }, |
| 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 | + }, |
1997 | 2044 | { |
1998 | 2045 | "cell_type": "code", |
1999 | 2046 | "execution_count": null, |
|
0 commit comments