Skip to content

Instantly share code, notes, and snippets.

@wesinator
Last active May 24, 2020 16:52
Show Gist options
  • Select an option

  • Save wesinator/3545871bd6af1ce35c80d8c0fcd7bc2c to your computer and use it in GitHub Desktop.

Select an option

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
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