Last active
May 24, 2020 16:52
-
-
Save wesinator/3545871bd6af1ce35c80d8c0fcd7bc2c to your computer and use it in GitHub Desktop.
Code to count occurrences of unique keys, within object dicts, across list of multiple objects
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| def sort_dict(dictionary): | |
| # https://stackoverflow.com/questions/613183/how-do-i-sort-a-dictionary-by-value | |
| return dict(sorted(dictionary.items(), key=lambda kv: kv[1], reverse=True)) | |
| # Code to count occurrences of unique keys, within object dicts, across list of multiple objects | |
| def count_occurrences_keys(objects_list): | |
| counts = {} | |
| for object in objects_list: | |
| for key in object.object_dict: | |
| counts[key] = counts.get(key, 0) + 1 | |
| return sort_dict(counts) | |
| def dict_string(dictionary): | |
| #print(dictionary) | |
| s = "" | |
| for key in dictionary: | |
| s += "{}: {}\n".format(str(key), str(dictionary[key])) | |
| return s |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment