Skip to content

Commit 938577b

Browse files
feat: count_thresholds - Count the number of elements between certain thresholds
1 parent 4f8c1bf commit 938577b

File tree

6 files changed

+20
-0
lines changed

6 files changed

+20
-0
lines changed

ebook/chapters/standard_lib_chapters/non_categorized.tex

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -223,3 +223,17 @@ \subsection{Slicing Generators}
223223
Making use of the awesome \lstinline{itertools} module, you can!
224224

225225
\lstinputlisting[caption=slice\_generators.py]{../standard_lib/slice_generators.py}
226+
227+
228+
\subsection{Count Elements Between Thresholds}
229+
230+
If you want to get the number of elements between certain thresholds, you can make use of the following Listing.
231+
232+
\lstinputlisting[caption=count\_thresholds.py]{../standard_lib/count_thresholds.py}
233+
234+
The essential point is, that you can sum booleans as they are a subclass of integers.
235+
236+
\begin{lstlisting}[caption=Output of count\_thresholds.py]
237+
$ python count_thresholds.py
238+
3
239+
\end{lstlisting}

ebook/python-snippets.epub

307 Bytes
Binary file not shown.

ebook/python-snippets.mobi

908 Bytes
Binary file not shown.

ebook/python-snippets.pdf

1.21 KB
Binary file not shown.

standard_lib/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ A collection of useful snippets using only the standard library.
1515
| capture_output | Capture the output of a function generally directing to stdout |
1616
| check_pattern | Check for multiple string patterns |
1717
| compare_strings | Use `difflib.SequenceMatcher` to compare strings |
18+
| count_thresholds | Count the number of elements between certain thresholds |
1819
| crazy_dict_expression | The craziest dictionary expression ever seen |
1920
| deprecated_decorator | Prints a DeprecationWarning when using a function/method marked as deprecated |
2021
| dict_based_on_lists | Create a dict based on two lists |

standard_lib/count_thresholds.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
data = [1, 2, 3, 4, 5, 6, 7]
2+
low, high = 3, 6
3+
result = sum(low <= x < high for x in data)
4+
5+
print(result)

0 commit comments

Comments
 (0)