From 61901120ad61047cb0ab4ebdfc95e9f3a4a1f968 Mon Sep 17 00:00:00 2001 From: eslopfer Date: Mon, 30 Jan 2023 20:34:13 +0000 Subject: [PATCH] docs(string_tokenizer_count): add example of how to sort dict --- .../devops/string_tokenizer_count/README.md | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/subjects/devops/string_tokenizer_count/README.md b/subjects/devops/string_tokenizer_count/README.md index df00a4879..205393121 100644 --- a/subjects/devops/string_tokenizer_count/README.md +++ b/subjects/devops/string_tokenizer_count/README.md @@ -30,10 +30,26 @@ $ - The `re` module can be used to remove non-alphanumeric characters. -- The `collections` module can be used to count the words. +- The `Counter` class of the `collections` module can be used to count the words. - The `operator` module can be used to sort the dictionary alphabetically by word. +Here is an example of how to sort a dictionary in python, using a test.py script: + +```python +dictionary = {'a': 5, 'c': 1, 'b': 3} +sorted_dict = dict(sorted(d.items(), key=lambda item: item[1])) +print(sorted_dict) +``` + +And its output: + +```console +$ python3 ./test.py +{'c': 1, 'b': 3, 'a': 5} +$ +``` + ### References - [`re` module](https://docs.python.org/3/library/re.html)