Velocity Reviews > Increase value in hash table

Increase value in hash table

moonhkt
Guest
Posts: n/a

 01-23-2013
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'

Chris Rebert
Guest
Posts: n/a

 01-23-2013
On Jan 22, 2013 11:31 PM, "moonhkt" <(E-Mail Removed)> 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/c...ctions.Counter

moonhkt
Guest
Posts: n/a

 01-23-2013
On Jan 23, 3:34*pm, Chris Rebert <(E-Mail Removed)> wrote:
> On Jan 22, 2013 11:31 PM, "moonhkt" <(E-Mail Removed)> 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/c...ctions.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'

Oscar Benjamin
Guest
Posts: n/a

 01-23-2013
On 23 January 2013 07:26, moonhkt <(E-Mail Removed)> 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

Steven D'Aprano
Guest
Posts: n/a

 01-23-2013
On Wed, 23 Jan 2013 10:12:25 +0000, Oscar Benjamin wrote:

> 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

Another way of writing that is:

printque[val] = printque.get(val, 0) + 1

--
Steven

Steven D'Aprano
Guest
Posts: n/a

 01-23-2013
On Wed, 23 Jan 2013 10:12:25 +0000, Oscar Benjamin wrote:

> 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

Another way of writing that is:

printque[val] = printque.get(val, 0) + 1

--
Steven

Steven D'Aprano
Guest
Posts: n/a

 01-23-2013
On Wed, 23 Jan 2013 10:12:25 +0000, Oscar Benjamin wrote:

> 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

Another way of writing that is:

printque[val] = printque.get(val, 0) + 1

--
Steven

Steven D'Aprano
Guest
Posts: n/a

 01-23-2013
On Wed, 23 Jan 2013 10:12:25 +0000, Oscar Benjamin wrote:

> 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

Another way of writing that is:

printque[val] = printque.get(val, 0) + 1

--
Steven

Steven D'Aprano
Guest
Posts: n/a

 01-23-2013
Steven D'Aprano wrote:
[snip content]

Arrgggh, it's happened again. Sorry for the multiple posts folks, I *swear*
I only sent it once.

Trying this time with a different news client.

--
Steven

rusi
Guest
Posts: n/a

 01-23-2013
On Jan 23, 3:54*pm, Steven D'Aprano <steve
(E-Mail Removed)> wrote:
> Steven D'Aprano wrote:
>

> I *swear* I only sent it once.

Now Now Steven! Good boys dont swear.

> Arrgggh, it's happened again. Sorry for the multiple posts folks...
> Trying this time with a different news client.

Its a law of the universe called karma.
Thou shalt double triple quadruple post for each GG user thou tickest
off.
And the choice for instant liberation is always there:

Use GG!!