4 examples of 'python dictionary count values per key' in Python

Every line of 'python dictionary count values per key' code snippets is scanned for vulnerabilities by our powerful machine learning engine that combs millions of open source libraries, ensuring your Python code is secure.

All examples are scanned by Snyk Code

By copying the Snyk Code Snippets you agree to
29def count_value(lst, key):
30 d = {}
31 for obj in lst:
32 val = obj[key]
33 if val in d:
34 d[val] = d[val] + 1
35 else:
36 d[val] = 1
37 return d.iteritems()
4def increment_count(count_dict, key, n=1):
5 """
6 Puts the key in the dictionary if does not exist or adds one if it does.
7 Args:
8 count_dict: a dictionary mapping a string to an integer
9 key: a string
10 """
11 if key in count_dict:
12 count_dict[key] += n
13 else:
14 count_dict[key] = n
154def sort_dict(key_counts, by_key=False):
155 '''Accept a dict of key:values (numerics) returning list of key-value tuples ordered by desc values.
156
157 If by_key=True, sorts by dict key.'''
158 sort_key = lambda x: (-1 * x[1], x[0])
159 if by_key:
160 sort_key = lambda x: x[0]
161 return sorted(key_counts.items(), key=sort_key)
52def count (self):
53 return (self.__payload.count() * len(self.__encoders)) if self.__encoders else self.__payload.count()

Related snippets