<jupyter_start><jupyter_text>```class collections.Counter([iterable-or-mapping])A Counter is a dict subclass for counting hashable objects. It is an unorderedcollection where elements are stored as dictionary keys and theircounts are stored as dictionary values. Counts are allowed to be anyinteger value including zero or negative counts. The Counter class issimilar to bags or multisets in other languages.Elements are counted from an iterable or initialized from another mapping (or counter):```<jupyter_code>c = collect.Counter() # a new, empty counter
c = collect.Counter('gallahad') # a new counter from an iterable
c = collect.Counter({'red':4, 'blue':2}) # a new counter from a mapping
c = collect.Counter(cats=4, dogs=8) # a new counter from keyword args<jupyter_output><empty_output><jupyter_text>Counter objects have a dictionary interface except that they return a zero count for missing items instead of raising a KeyError:<jupyter_code>c = collect.Counter(['eggs', 'ham'])
c['fish']<jupyter_output><empty_output><jupyter_text>Setting a count to zero does not remove an element from a counter. Use del to remove it entirely:<jupyter_code>c['sausage'] = 0 # counter entry with a zero count
del c['sausage']<jupyter_output><empty_output><jupyter_text>**most_common([n])**Return a list of the n most common elements and their counts from the most common to the least. If n is omitted or None, most_common() returns all elements in the counter. Elements with equal counts are ordered arbitrarily:<jupyter_code>collect.Counter('abracadabra').most_common(3)<jupyter_output><empty_output><jupyter_text>**elements()**Return an iterator over elements repeating each as many times as its count. Elements are returned in arbitrary order. If an element’s count is less than one, elements() will ignore it.<jupyter_code>c = collect.Counter(a=4, b=2, c=0, d=-2)
l = list(c.elements())
l
sorted(l)<jupyter_output><empty_output><jupyter_text>**subtract([iterable-or-mapping])**Elements are subtracted from an iterable or from another mapping (or counter). Like dict.update() but subtracts counts instead of replacing them. Both inputs and outputs may be zero or negative.<jupyter_code>c = collect.Counter(a=4, b=2, c=0, d=-2)
c
d = collect.Counter(a=1, b=2, c=3, d=4)
c
c.subtract(d)
c<jupyter_output><empty_output><jupyter_text>The usual dictionary methods are available for Counter objects except for two which work differently for counters.**fromkeys**(iterable)This class method is not implemented for Counter objects.**update**([iterable-or-mapping])Elements are counted from an iterable or added-in from another mapping (or counter). Like dict.update() but adds counts instead of replacing them. Also, the iterable is expected to be a sequence of elements, not a sequence of (key, value) pairs.<jupyter_code>sum(c.values()) # total of all counts
c.clear() # reset all counts
list(c) # list unique elements
set(c) # convert to a set
dict(c) # convert to a regular dictionary
c.items() # convert to a list of (elem, cnt) pairs
# Counter(dict(list_of_pairs)) #convert from a list of (elem, cnt) pair
#  c.most_common()[:-n-1:-1]   --- n least common elements
[1,2,3,4,5,6,7][: -4 : -1]
c = collect.Counter({'a': 3, 'b': 0, 'c': -3, 'd': -6})
c = +c # remover zero and negative counts
c<jupyter_output><empty_output><jupyter_text>Several mathematical operations are provided for combining Counter objects to produce multisets (counters that have counts greater than zero). Addition and subtraction combine counters by adding or subtracting the counts of corresponding elements. Intersection and union return the minimum and maximum of corresponding counts. Each operation can accept inputs with signed counts, but the output will exclude results with counts of zero or less.<jupyter_code>c = collect.Counter(a =3, b=1)
d = collect.Counter(a=1, b=2)
c +d  # add two counters togeter: c[x] + d[x]
c-d # subtract (keeping only positive counts)
c & d # intersection: min(c[x], d[x])
c | d # union: max(c[x, d[x]])<jupyter_output><empty_output><jupyter_text>Unary addition and subtraction are shortcuts for adding an empty counter or subtracting from an empty counter.<jupyter_code>c = collect.Counter(a=2, b=-4)
+c
-c<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.10396039603960396, 'fraction_numerical': 0.009193776520509194, 'mean_word_length': 5.899186991869919, 'pattern_counts': {'":': 0, '<': 37, '<?xml version=': 0, '>': 37, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '9', 'dir': 'jupyter-structured-clean-dedup', 'id': '595340', 'license': 'MIT', 'n_tokens_mistral': 1295, 'n_tokens_neox': 1256, 'n_words': 505, 'path': 'Spark_and_Python_For_Big_Data_with_PySpark/01-python_libr/collections.Counter.ipynb', 'repo_name': 'ashishpatel26/Machine-Learning-and-Quantitative-analysis', 'size': '4242'}