3

When I print I get:

Counter({'pit': 6, 'mike': 4, 'andy': 3, 'jose': 2})
<class 'collections.Counter'>

How can I convert the result into:

pit = 6
mike = 4
andy = 3
jose = 2

Or a text file that shows:

 pit    6
 mike   4
 andy   3
 jose   2
Aurora
  • 4,384
  • 3
  • 34
  • 44
Andrés Muñoz
  • 225
  • 2
  • 10

2 Answers2

5
for k,v in myCounter.iteritems():
  print "%s = %s" %(k, v)
inspectorG4dget
  • 110,290
  • 27
  • 149
  • 241
1

If you want you can extend dict object and override the __str__() function to something like this :

def __str__(self):
    out = ''
    for k, v in self.iteritems():
        out += "%s\t%s" % (k, v)
    return out

This should output what you want each time, the object is represented or printed.

Emil Davtyan
  • 13,808
  • 5
  • 44
  • 66