Increase value in hash table

Oscar Benjamin oscar.j.benjamin at gmail.com
Wed Jan 23 11:12:25 CET 2013


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



More information about the Python-list mailing list