Increase value in hash table

moonhk moonhkt at gmail.com
Wed Jan 23 10:33:09 EST 2013


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.benjamin at gmail.com> wrote:
> On 23 January 2013 07:26, moonhkt <moonhkt 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



More information about the Python-list mailing list