Skip to content

Commit

Permalink
Atomic examples
Browse files Browse the repository at this point in the history
  • Loading branch information
s2t2 committed Sep 11, 2024
1 parent a01f221 commit 67fc41f
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 3 deletions.
11 changes: 8 additions & 3 deletions docs/notes/data-processing/filtering.qmd
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,7 @@ The simplest way to implement this is by introducing an \"if\" statement into th

```{python}
my_numbers = [1, 2, 3, 4, 5, 6, 7]
print("ORIGINAL:", my_numbers)
```
```{python}
for n in my_numbers:
if n > 3: # FILTER CONDITION
print(n)
Expand All @@ -32,6 +29,8 @@ We see we are only printing numbers that match the condition.
However in this case we lose access to the matching items. To retain access for later, we can implement a familiar collection operation using the `append` method, similar to the [mapping](./mapping.qmd) operation:

```{python}
my_numbers = [1, 2, 3, 4, 5, 6, 7]
matching_nums = []
for n in my_numbers:
Expand All @@ -45,6 +44,8 @@ print(matching_nums)
After performing a filter operation, we wind up with a subset of the data. Depending on the condition, is possible for the resulting list to contain no items, one item, some items, or all items:

```{python}
my_numbers = [1, 2, 3, 4, 5, 6, 7]
matching_nums = []
for n in my_numbers:
Expand All @@ -55,6 +56,8 @@ print(matching_nums)
```

```{python}
my_numbers = [1, 2, 3, 4, 5, 6, 7]
matching_nums = []
for n in my_numbers:
Expand All @@ -65,6 +68,8 @@ print(matching_nums)
```

```{python}
my_numbers = [1, 2, 3, 4, 5, 6, 7]
matching_nums = []
for n in my_numbers:
Expand Down
12 changes: 12 additions & 0 deletions docs/notes/data-processing/list-comprehensions.qmd
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ print(my_numbers)
Mapping, the long way:

```{python}
my_numbers = [1, 2, 3, 4, 5, 6, 7]
transformed_nums = []
for n in my_numbers:
Expand All @@ -37,6 +39,8 @@ print(transformed_nums)
Mapping, the short way, using a list comprehension (equivalent):

```{python}
my_numbers = [1, 2, 3, 4, 5, 6, 7]
# NEW_LIST = [VALUE_TO_COLLECT for ITEM in EXISTING_LIST]
transformed_nums = [n * 100 for n in my_numbers]
Expand All @@ -51,6 +55,8 @@ We optionally add an `if` clause to also implement filtering:
Filtering, the long way:

```{python}
my_numbers = [1, 2, 3, 4, 5, 6, 7]
matching_nums = []
for n in my_numbers:
Expand All @@ -63,6 +69,8 @@ print(matching_nums)
Filtering, the short way, using a list comprehension (equivalent):

```{python}
my_numbers = [1, 2, 3, 4, 5, 6, 7]
# NEW_LIST = [VALUE_TO_COLLECT for ITEM in EXISTING_LIST if CONDITION]
matching_nums = [n for n in my_numbers if n > 3]
Expand All @@ -76,6 +84,8 @@ We can mix and match techniques to perform both mapping and filtering, as desire
Filtering and mapping, the long way:

```{python}
my_numbers = [1, 2, 3, 4, 5, 6, 7]
matching_nums = []
for n in my_numbers:
Expand All @@ -88,6 +98,8 @@ print(matching_nums)
Filtering and mapping, the short way, using a list comprehension (equivalent):

```{python}
my_numbers = [1, 2, 3, 4, 5, 6, 7]
new_nums = [n * 100 for n in my_numbers if n > 3]
print(new_nums)
```
Expand Down

0 comments on commit 67fc41f

Please sign in to comment.