#https://pymotw.com/2/collections/counter.html


'''

Counter :

Counter is a dict subclass which helps to count hashable objects.
 Inside it elements are stored as dictionary keys and counts are stored as values which can be zero or negative.

A Counter is a container that keeps track of how many times equivalent values are added.
 It can be used to implement the same algorithms for which bag or multiset
 data structures are commonly used in other languages.






 https://pymotw.com/2/collections/counter.html



'''

import collections
list = [ "india","usa","canada","india","newyork"]
cnt = collections.Counter(list)
for l in list:
    cnt[l]+=1
print cnt    #Counter({'india': 4, 'canada': 2, 'newyork': 2, 'usa': 2})

#Once a Counter is populated, its values can be retrieved using the dictionary API.
c = collections.Counter('abcdaab')
for letter in c:
    print '%s : %d' % (letter, c[letter])
    '''
    O/P :
    a : 3
    c : 1
    b : 2
    d : 1
    '''




'''

most_common is a method which returns
most common elements and their counts from the
most common to the least.
>> collections.Counter('abracadabra')
>> Counter({'a': 5, 'b': 2, 'r': 2, 'c': 1, 'd': 1})

>>> Counter('abracadabra').most_common(3)
>>[('a', 5), ('r', 2), ('b', 2)]


'''



# count number of extendtion in file :

import os
flist=[]
for root, dirs, files in os.walk(".", topdown=False):
    for name in files:
        print("Files",os.path.join(root, name))
        flist.append(os.path.join(root, name))
    for name in dirs:
        print("Dirc" ,os.path.join(root, name))

fc=collections.Counter(flist)
for f in flist:
    print '%s : %d' % (f, fc[f])

