Increase value in hash table

moonhkt moonhkt at gmail.com
Wed Jan 23 16:39:27 CET 2013


On Jan 23, 11:33 pm, moonhk <moon... at gmail.com> wrote:
> Works.
>
>      prndev = line.split()
>          # print line
>          for key in prndev :
>              if key in 'lpr':
>                 val = prndev[5].replace("-P","")
>                 if val not in printque:
>                    printque[val] = 1
>                 else:
>                    printque[val] = printque[val] + 1
>              if key in "/dev/null":
>                  val='null'
>                  if val not in printque:
>                     printque[val] = 1
>                  else:
>                     printque[val] = printque[val] + 1
>
> On Wed, Jan 23, 2013 at 6:12 PM, Oscar Benjamin
>
>
>
>
>
>
>
>
>
> <oscar.j.benja... at gmail.com> wrote:
> > On 23 January 2013 07:26, 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
>
> >> # Global Veriable
> >> printque = {}
> >> in def have below
>
> >> printque[val] =  printque[val] + 1
>
> >> I have below error
> >>   File "xprintlogchk.py", line 78, in chklog
> >>     printque[val] =  printque[val] + 1
> >> KeyError: 'nan'
>
> > You can't retrieve the value of printque[val] if you haven't yet added
> > an entry with the key val to the dict. Try this:
>
> > if val not in printque:
> >     printque[val] = 1
> > else:
> >     printque[val] = printque[val] + 1
>
> > Oscar
>
> --
> moonhkt
> GMT+8

Tried below works
  a = "A B"
   karry = a.split()
   age[karry[0]] = age.get(karry[0], 100) + 1
   age[karry[1]] = age.get(karry[1], 0) + 1
   age[karry[1]] = age.get(karry[1], 0) + 1


Result
A 101
B 2



More information about the Python-list mailing list