Given an iterable sequence, count the number of occurrences of the values in it:
Method 1:
def get_counts(sequence): counts = {} for x in sequence: if x in counts: counts[x] += 1 else: counts[x] = 1 return counts
Method 2:
Utilizing the built-in collections in python
from collections import defaultdict def get_counts2(sequence): counts = defaultdict(int) # All values will be initialized to 0 for x in sequence: counts[x] +=1 return counts
Method 3:
from collections import Counter counts = Counter(sequence) # where you can use counts.most_common(10) to sort the top ten occurrences in reverse order
The resulting statistics are then sorted:
def top_count(count_dic, n=10): # Defaults to the largest n=10 values value_key_pairs = [(count,data) for counts,data in cout_dict.items()] value_key_pairs.sort() #sorted(value_key_pairs) both return value_key_pairs[-n:]
Above this python to a given iterable set of statistical frequency of occurrence, and sorting method is all that I share with you, I hope to give you a reference, and I hope you support me more.