Increase value in hash table

M

moonhkt

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'
 
M

moonhkt


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'
 
O

Oscar Benjamin

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
 
S

Steven D'Aprano

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
 
S

Steven D'Aprano

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
 
S

Steven D'Aprano

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
 
S

Steven D'Aprano

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
 
S

Steven D'Aprano

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.
 
R

rusi

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!!
 
M

moonhk

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


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
 
M

moonhkt

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

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

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
 
D

Dave Angel

For some definition of 'works"


This test will fire if key is the letter "l", or the letter "p", or the
letter "r". Probably not what you want. Suggest you change it to
if key == "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":

ditto here
val='null'
if val not in printque:
printque[val] = 1
else:
printque[val] = printque[val] + 1

Of course, I don't know what prndev actually looks like, so I could be
wrong as well. But I doubt it "works" as written.
 
V

Vito De Tullio

moonhkt said:
Data file
V1
V2
V3
V4
V4
V3

How to using count number of data ?

Output
V1 = 1
V2 = 1
V3 =2
V4 = 2

import collections

with open(data_file) as f:
print(collections.Counter(f.readlines()))


it's a start
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,763
Messages
2,569,563
Members
45,039
Latest member
CasimiraVa

Latest Threads

Top