Skip to content

Commit cc473f8

Browse files
add content
1 parent bb66b96 commit cc473f8

22 files changed

+2916
-158
lines changed

Chapter2/itertools.ipynb

+47-32
Original file line numberDiff line numberDiff line change
@@ -654,67 +654,82 @@
654654
"id": "16243084",
655655
"metadata": {},
656656
"source": [
657-
"### itertools.dropwhile: Drop Elements in an Iterable Until a Condition Is False"
657+
"### Python's dropwhile: A Clean Approach to Sequential Filtering"
658658
]
659659
},
660660
{
661-
"attachments": {},
662661
"cell_type": "markdown",
663-
"id": "b5d8c554",
662+
"id": "095d11d6-df75-464a-9623-cee19e91b4b6",
664663
"metadata": {},
665664
"source": [
666-
"If you want to drop elements from an iterable until a condition is false, use `itertools.dropwhile`."
665+
"Repeatedly checking elements in an iteration until a condition is met results in verbose and less readable code, especially when you want to skip elements at the beginning of a sequence."
667666
]
668667
},
669668
{
670669
"cell_type": "code",
671-
"execution_count": 12,
672-
"id": "cd9d5db3",
670+
"execution_count": 8,
671+
"id": "e68b75f2-97cb-445d-938e-1d7e3dc54222",
673672
"metadata": {},
674673
"outputs": [
675674
{
676-
"data": {
677-
"text/plain": [
678-
"[5, 2, 4]"
679-
]
680-
},
681-
"execution_count": 12,
682-
"metadata": {},
683-
"output_type": "execute_result"
675+
"name": "stdout",
676+
"output_type": "stream",
677+
"text": [
678+
"ABC456\n"
679+
]
684680
}
685681
],
686682
"source": [
687-
"from itertools import dropwhile\n",
683+
"text = \"123ABC456\"\n",
684+
"alpha_part = []\n",
685+
"found_alpha = False\n",
688686
"\n",
689-
"nums = [1, 2, 5, 2, 4]\n",
687+
"for char in text:\n",
688+
" if found_alpha or not char.isdigit():\n",
689+
" found_alpha = True\n",
690+
" alpha_part.append(char)\n",
690691
"\n",
691-
"# Drop every number until a number >= 5\n",
692-
"list(dropwhile(lambda n: n < 5, nums))"
692+
"\n",
693+
"print(''.join(alpha_part))"
694+
]
695+
},
696+
{
697+
"cell_type": "markdown",
698+
"id": "9fff7ed4-d7f6-4862-934d-61bfa1728a89",
699+
"metadata": {},
700+
"source": [
701+
"With `itertools.dropwhile`, you can elegantly skip elements from the beginning of an iteration until a condition becomes False, making your code more concise and functional."
693702
]
694703
},
695704
{
696705
"cell_type": "code",
697-
"execution_count": 13,
698-
"id": "e29b4c7f",
706+
"execution_count": 7,
707+
"id": "a1464951-36c2-4086-8647-dd388633bfb9",
699708
"metadata": {},
700709
"outputs": [
701710
{
702-
"data": {
703-
"text/plain": [
704-
"'Nice!'"
705-
]
706-
},
707-
"execution_count": 13,
708-
"metadata": {},
709-
"output_type": "execute_result"
711+
"name": "stdout",
712+
"output_type": "stream",
713+
"text": [
714+
"ABC456\n"
715+
]
710716
}
711717
],
712718
"source": [
713-
"word = 'abcNice!'\n",
719+
"from itertools import dropwhile\n",
720+
"\n",
721+
"text = \"123ABC456\"\n",
722+
"alpha_part = dropwhile(str.isdigit, text)\n",
714723
"\n",
715-
"# Drop every char until a char is in upper case\n",
716-
"chars = dropwhile(lambda char: char.islower(), word)\n",
717-
"''.join(chars)"
724+
"print(''.join(alpha_part))"
725+
]
726+
},
727+
{
728+
"cell_type": "markdown",
729+
"id": "e7b1b11a-97b0-4a2e-9e78-4ceed235d52a",
730+
"metadata": {},
731+
"source": [
732+
"In this example, `dropwhile` takes a predicate function (`str.isdigit`) and the text string. It skips digits until it finds a non-digit character, then returns the rest of the string. "
718733
]
719734
},
720735
{

0 commit comments

Comments
 (0)