|
654 | 654 | "id": "16243084",
|
655 | 655 | "metadata": {},
|
656 | 656 | "source": [
|
657 |
| - "### itertools.dropwhile: Drop Elements in an Iterable Until a Condition Is False" |
| 657 | + "### Python's dropwhile: A Clean Approach to Sequential Filtering" |
658 | 658 | ]
|
659 | 659 | },
|
660 | 660 | {
|
661 |
| - "attachments": {}, |
662 | 661 | "cell_type": "markdown",
|
663 |
| - "id": "b5d8c554", |
| 662 | + "id": "095d11d6-df75-464a-9623-cee19e91b4b6", |
664 | 663 | "metadata": {},
|
665 | 664 | "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." |
667 | 666 | ]
|
668 | 667 | },
|
669 | 668 | {
|
670 | 669 | "cell_type": "code",
|
671 |
| - "execution_count": 12, |
672 |
| - "id": "cd9d5db3", |
| 670 | + "execution_count": 8, |
| 671 | + "id": "e68b75f2-97cb-445d-938e-1d7e3dc54222", |
673 | 672 | "metadata": {},
|
674 | 673 | "outputs": [
|
675 | 674 | {
|
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 | + ] |
684 | 680 | }
|
685 | 681 | ],
|
686 | 682 | "source": [
|
687 |
| - "from itertools import dropwhile\n", |
| 683 | + "text = \"123ABC456\"\n", |
| 684 | + "alpha_part = []\n", |
| 685 | + "found_alpha = False\n", |
688 | 686 | "\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", |
690 | 691 | "\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." |
693 | 702 | ]
|
694 | 703 | },
|
695 | 704 | {
|
696 | 705 | "cell_type": "code",
|
697 |
| - "execution_count": 13, |
698 |
| - "id": "e29b4c7f", |
| 706 | + "execution_count": 7, |
| 707 | + "id": "a1464951-36c2-4086-8647-dd388633bfb9", |
699 | 708 | "metadata": {},
|
700 | 709 | "outputs": [
|
701 | 710 | {
|
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 | + ] |
710 | 716 | }
|
711 | 717 | ],
|
712 | 718 | "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", |
714 | 723 | "\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. " |
718 | 733 | ]
|
719 | 734 | },
|
720 | 735 | {
|
|
0 commit comments