Increase value in hash table

moonhkt moonhkt at gmail.com
Wed Jan 23 10:23:04 CET 2013


On Jan 23, 3:34 pm, Chris Rebert <c... at rebertia.com> wrote:
> On Jan 22, 2013 11:31 PM, "moonhkt" <moon... at gmail.com> wrote:
>
>
>
>
>
>
>
>
>
>
>
> > Hi Al
>
> > I have Data file have below
>
> > Data file
> > V1
> > V2
> > V3
> > V4
> > V4
> > V3
>
> > How to using count number of data ?
>
> > Output
> > V1 = 1
> > V2 = 1
> > V3 =2
> > V4 = 2
>
> Construct a frequency table using collections.Counter:
>
> http://docs.python.org/2.7/library/collections.html#collections.Counter

What is problem for below ?
#!/usr/bin/env python
# Python hash {}
# Python Lists  []

global age
karry = "ER"
k1 = "EU"
age = {}
age[karry] = 3
age[k1] = 5

def ABC():
   global age
   global karry
   i = 0
   a = "A B"
   karry = a.split()
   age[karry[0]] += 1


ABC()
for key in age:
   print key,  age[key]



Result
ex_array.py
Traceback (most recent call last):
  File "ex_array.py", line 21, in <module>
    ABC()
  File "ex_array.py", line 18, in ABC
    age[karry[0]] += 1
KeyError: 'A'




More information about the Python-list mailing list