Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   Python (http://www.velocityreviews.com/forums/f43-python.html)
-   -   Increase value in hash table (http://www.velocityreviews.com/forums/t956823-increase-value-in-hash-table.html)

moonhkt 01-23-2013 07:26 AM

Increase value in hash table
 
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 01-23-2013 07:34 AM

Re: Increase value in hash table
 
On Jan 22, 2013 11:31 PM, "moonhkt" <moonhkt@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/c...ctions.Counter


moonhkt 01-23-2013 09:23 AM

Re: Increase value in hash table
 
On Jan 23, 3:34*pm, Chris Rebert <c...@rebertia.com> wrote:
> On Jan 22, 2013 11:31 PM, "moonhkt" <moon...@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/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 01-23-2013 10:12 AM

Re: Increase value in hash table
 
On 23 January 2013 07:26, moonhkt <moonhkt@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

Steven D'Aprano 01-23-2013 10:46 AM

Re: Increase value in hash table
 
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 01-23-2013 10:46 AM

Re: Increase value in hash table
 
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 01-23-2013 10:47 AM

Re: Increase value in hash table
 
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 01-23-2013 10:47 AM

Re: Increase value in hash table
 
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 01-23-2013 10:54 AM

Multiple postings [was Re: Increase value in hash table]
 
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 01-23-2013 02:00 PM

Re: Multiple postings [was Re: Increase value in hash table]
 
On Jan 23, 3:54*pm, Steven D'Aprano <steve
+comp.lang.pyt...@pearwood.info> 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!!


All times are GMT. The time now is 03:36 AM.

Powered by vBulletin®. Copyright ©2000 - 2013, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.