|
2 | 2 | "cells": [
|
3 | 3 | {
|
4 | 4 | "cell_type": "markdown",
|
5 |
| - "source": "[](https://github.com/khuyentran1401/Data-science/blob/master/productive_tools/pipe.ipynb)\n\n[<img src=\"https://deepnote.com/buttons/launch-in-deepnote.svg\">](/work/Data-science/productive_tools/pipe.ipynb)", |
| 5 | + "source": "[](https://github.com/khuyentran1401/Data-science/blob/master/productive_tools/pipe.ipynb)\n\n[<img src=\"https://deepnote.com/buttons/launch-in-deepnote.svg\">](https://deepnote.com/project/Data-science-hxlyJpi-QrKFJziQgoMSmQ/%2FData-science%2Fproductive_tools%2Fpipe.ipynb)", |
6 | 6 | "metadata": {
|
7 | 7 | "tags": [],
|
8 | 8 | "cell_id": "00000-2c4341ee-abf7-4f9b-af8d-48afb0d90081",
|
9 | 9 | "deepnote_cell_type": "markdown"
|
10 |
| - }, |
11 |
| - "outputs": [], |
12 |
| - "execution_count": null |
| 10 | + } |
13 | 11 | },
|
14 | 12 | {
|
15 | 13 | "cell_type": "code",
|
|
52 | 50 | "deepnote_cell_type": "code"
|
53 | 51 | },
|
54 | 52 | "source": "from pipe import where",
|
55 |
| - "execution_count": 1, |
| 53 | + "execution_count": null, |
56 | 54 | "outputs": []
|
57 | 55 | },
|
58 | 56 | {
|
|
66 | 64 | "deepnote_cell_type": "code"
|
67 | 65 | },
|
68 | 66 | "source": "arr = [1, 2, 3, 4, 5]\n\n# filter using list comprehension\n[x for x in arr if x % 2 == 0]",
|
69 |
| - "execution_count": 2, |
| 67 | + "execution_count": null, |
70 | 68 | "outputs": [
|
71 | 69 | {
|
72 | 70 | "data": {
|
|
89 | 87 | "deepnote_cell_type": "code"
|
90 | 88 | },
|
91 | 89 | "source": "# filter using pipe\nlist(arr | where(lambda x: x % 2 == 0))",
|
92 |
| - "execution_count": 3, |
| 90 | + "execution_count": null, |
93 | 91 | "outputs": [
|
94 | 92 | {
|
95 | 93 | "data": {
|
|
120 | 118 | "deepnote_cell_type": "code"
|
121 | 119 | },
|
122 | 120 | "source": "from pipe import select",
|
123 |
| - "execution_count": 2, |
| 121 | + "execution_count": null, |
124 | 122 | "outputs": []
|
125 | 123 | },
|
126 | 124 | {
|
|
134 | 132 | "deepnote_cell_type": "code"
|
135 | 133 | },
|
136 | 134 | "source": "arr = [1, 2, 3, 4, 5]",
|
137 |
| - "execution_count": 45, |
| 135 | + "execution_count": null, |
138 | 136 | "outputs": [
|
139 | 137 | {
|
140 | 138 | "data": {
|
|
157 | 155 | "deepnote_cell_type": "code"
|
158 | 156 | },
|
159 | 157 | "source": "list(arr | select(lambda x: x * 2))",
|
160 |
| - "execution_count": 47, |
| 158 | + "execution_count": null, |
161 | 159 | "outputs": [
|
162 | 160 | {
|
163 | 161 | "data": {
|
|
188 | 186 | "deepnote_cell_type": "code"
|
189 | 187 | },
|
190 | 188 | "source": "# Instead of this\nlist(map(lambda x: x * 2, filter(lambda x: x % 2 == 0, arr)))",
|
191 |
| - "execution_count": 46, |
| 189 | + "execution_count": null, |
192 | 190 | "outputs": [
|
193 | 191 | {
|
194 | 192 | "data": {
|
|
219 | 217 | "deepnote_cell_type": "code"
|
220 | 218 | },
|
221 | 219 | "source": "# use pipe\nlist(arr | where(lambda x: x % 2 == 0) | select(lambda x: x * 2))",
|
222 |
| - "execution_count": 7, |
| 220 | + "execution_count": null, |
223 | 221 | "outputs": [
|
224 | 222 | {
|
225 | 223 | "data": {
|
|
242 | 240 | "deepnote_cell_type": "code"
|
243 | 241 | },
|
244 | 242 | "source": "# the order matters\nlist(arr | select(lambda x: x * 2) | where(lambda x: x % 2 == 0))",
|
245 |
| - "execution_count": 8, |
| 243 | + "execution_count": null, |
246 | 244 | "outputs": [
|
247 | 245 | {
|
248 | 246 | "data": {
|
|
281 | 279 | "deepnote_cell_type": "code"
|
282 | 280 | },
|
283 | 281 | "source": "from pipe import chain",
|
284 |
| - "execution_count": 28, |
| 282 | + "execution_count": null, |
285 | 283 | "outputs": [
|
286 | 284 | {
|
287 | 285 | "data": {
|
|
304 | 302 | "deepnote_cell_type": "code"
|
305 | 303 | },
|
306 | 304 | "source": "nested = [[1, 2, [3]], [4, 5]]\nlist(nested | chain)",
|
307 |
| - "execution_count": 48, |
| 305 | + "execution_count": null, |
308 | 306 | "outputs": [
|
309 | 307 | {
|
310 | 308 | "data": {
|
|
343 | 341 | "deepnote_cell_type": "code"
|
344 | 342 | },
|
345 | 343 | "source": "from pipe import traverse",
|
346 |
| - "execution_count": 49, |
| 344 | + "execution_count": null, |
347 | 345 | "outputs": [
|
348 | 346 | {
|
349 | 347 | "data": {
|
|
366 | 364 | "deepnote_cell_type": "code"
|
367 | 365 | },
|
368 | 366 | "source": "list(nested | traverse)",
|
369 |
| - "execution_count": 50, |
| 367 | + "execution_count": null, |
370 | 368 | "outputs": [
|
371 | 369 | {
|
372 | 370 | "data": {
|
|
397 | 395 | "deepnote_cell_type": "code"
|
398 | 396 | },
|
399 | 397 | "source": "fruits = [\n {\"name\": \"apple\", \"price\": [2, 5]},\n {\"name\": \"orange\", \"price\": 4},\n {\"name\": \"grape\", \"price\": 5},\n]",
|
400 |
| - "execution_count": 27, |
| 398 | + "execution_count": null, |
401 | 399 | "outputs": [
|
402 | 400 | {
|
403 | 401 | "data": {
|
|
420 | 418 | "deepnote_cell_type": "code"
|
421 | 419 | },
|
422 | 420 | "source": "list(fruits | select(lambda fruit: fruit[\"price\"]) | traverse)",
|
423 |
| - "execution_count": 36, |
| 421 | + "execution_count": null, |
424 | 422 | "outputs": [
|
425 | 423 | {
|
426 | 424 | "data": {
|
|
459 | 457 | "deepnote_cell_type": "code"
|
460 | 458 | },
|
461 | 459 | "source": "from pipe import groupby",
|
462 |
| - "execution_count": 3, |
| 460 | + "execution_count": null, |
463 | 461 | "outputs": []
|
464 | 462 | },
|
465 | 463 | {
|
|
474 | 472 | "deepnote_cell_type": "code"
|
475 | 473 | },
|
476 | 474 | "source": "list(\n (1, 2, 3, 4, 5, 6, 7, 8, 9)\n | groupby(lambda x: \"Even\" if x % 2==0 else \"Odd\")\n | select(lambda x: {x[0]: list(x[1])})\n)",
|
477 |
| - "execution_count": 11, |
| 475 | + "execution_count": null, |
478 | 476 | "outputs": [
|
479 | 477 | {
|
480 | 478 | "data": {
|
|
497 | 495 | "deepnote_cell_type": "code"
|
498 | 496 | },
|
499 | 497 | "source": "list(\n (1, 2, 3, 4, 5, 6, 7, 8, 9)\n | groupby(lambda x: \"Even\" if x % 2==0 else \"Odd\")\n | select(lambda x: {x[0]: list(x[1] | where(lambda x: x > 2))})\n)",
|
500 |
| - "execution_count": 12, |
| 498 | + "execution_count": null, |
501 | 499 | "outputs": [
|
502 | 500 | {
|
503 | 501 | "data": {
|
|
528 | 526 | "deepnote_cell_type": "code"
|
529 | 527 | },
|
530 | 528 | "source": "from pipe import dedup\n\narr = [1, 2, 2, 3, 4, 5, 6, 6, 7, 9, 3, 3, 1]",
|
531 |
| - "execution_count": 15, |
| 529 | + "execution_count": null, |
532 | 530 | "outputs": [
|
533 | 531 | {
|
534 | 532 | "data": {
|
|
551 | 549 | "deepnote_cell_type": "code"
|
552 | 550 | },
|
553 | 551 | "source": "list(arr | dedup)",
|
554 |
| - "execution_count": 33, |
| 552 | + "execution_count": null, |
555 | 553 | "outputs": [
|
556 | 554 | {
|
557 | 555 | "data": {
|
|
574 | 572 | "deepnote_cell_type": "code"
|
575 | 573 | },
|
576 | 574 | "source": "# Get one element that is smaller than 5 and one element that is larger than or equal to 5\nlist(arr | dedup(lambda key: key < 5))",
|
577 |
| - "execution_count": 26, |
| 575 | + "execution_count": null, |
578 | 576 | "outputs": [
|
579 | 577 | {
|
580 | 578 | "data": {
|
|
605 | 603 | "deepnote_cell_type": "code"
|
606 | 604 | },
|
607 | 605 | "source": "data = [\n {\"name\": \"apple\", \"count\": 2},\n {\"name\": \"orange\", \"count\": 4},\n {\"name\": \"grape\", \"count\": None},\n {\"name\": \"orange\", \"count\": 7},\n]",
|
608 |
| - "execution_count": 12, |
| 606 | + "execution_count": null, |
609 | 607 | "outputs": [
|
610 | 608 | {
|
611 | 609 | "data": {
|
|
628 | 626 | "deepnote_cell_type": "code"
|
629 | 627 | },
|
630 | 628 | "source": "list(\n data\n | dedup(key=lambda fruit: fruit[\"name\"])\n | select(lambda fruit: fruit[\"count\"])\n | where(lambda count: isinstance(count, int))\n)",
|
631 |
| - "execution_count": 16, |
| 629 | + "execution_count": null, |
632 | 630 | "outputs": [
|
633 | 631 | {
|
634 | 632 | "data": {
|
|
0 commit comments