Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 938577b

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

File tree

6 files changed

+20
-0
lines changed
Filter options

6 files changed

+20
-0
lines changed

‎ebook/chapters/standard_lib_chapters/non_categorized.tex

Copy file name to clipboardExpand all lines: ebook/chapters/standard_lib_chapters/non_categorized.tex
+14Lines 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

Copy file name to clipboard
307 Bytes
Binary file not shown.

‎ebook/python-snippets.mobi

Copy file name to clipboard
908 Bytes
Binary file not shown.

‎ebook/python-snippets.pdf

Copy file name to clipboard
1.21 KB
Binary file not shown.

‎standard_lib/README.md

Copy file name to clipboardExpand all lines: standard_lib/README.md
+1Lines 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

Copy file name to clipboard
+5Lines 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 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.