Increase value in hash table

Discussion in 'Python' started by moonhkt, Jan 23, 2013.

  1. moonhkt

    moonhkt Guest

    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'
     
    moonhkt, Jan 23, 2013
    #1
    1. Advertising

  2. moonhkt

    Chris Rebert Guest

    Chris Rebert, Jan 23, 2013
    #2
    1. Advertising

  3. moonhkt

    moonhkt Guest

    On Jan 23, 3:34 pm, Chris Rebert <> wrote:
    > On Jan 22, 2013 11:31 PM, "moonhkt" <> 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/collections.html#collections.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'
     
    moonhkt, Jan 23, 2013
    #3
  4. On 23 January 2013 07:26, moonhkt <> 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
     
    Oscar Benjamin, Jan 23, 2013
    #4
  5. 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, Jan 23, 2013
    #5
  6. 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, Jan 23, 2013
    #6
  7. 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, Jan 23, 2013
    #7
  8. 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, Jan 23, 2013
    #8
  9. 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
     
    Steven D'Aprano, Jan 23, 2013
    #9
  10. moonhkt

    rusi Guest

    Re: Multiple postings [was Re: Increase value in hash table]

    On Jan 23, 3:54 pm, Steven D'Aprano <steve
    > 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!!
     
    rusi, Jan 23, 2013
    #10
  11. moonhkt

    moonhk Guest

    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
    <> wrote:
    > On 23 January 2013 07:26, moonhkt <> 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
     
    moonhk, Jan 23, 2013
    #11
  12. moonhkt

    moonhkt Guest

    On Jan 23, 11:33 pm, moonhk <> wrote:
    > 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
    >
    >
    >
    >
    >
    >
    >
    >
    >
    > <> wrote:
    > > On 23 January 2013 07:26, moonhkt <> 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


    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
     
    moonhkt, Jan 23, 2013
    #12
  13. moonhkt

    Dave Angel Guest

    On 01/23/2013 10:39 AM, moonhkt wrote:
    > On Jan 23, 11:33 pm, moonhk <> wrote:
    >> Works.


    For some definition of 'works"


    >>
    >> prndev = line.split()
    >> # print line
    >> for key in prndev :
    >> if key in 'lpr':


    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.

    --
    DaveA
     
    Dave Angel, Jan 23, 2013
    #13
  14. moonhkt wrote:

    > 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

    --
    ZeD
     
    Vito De Tullio, Jan 24, 2013
    #14
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Miguel Dias Moura

    How to increase this value by 1?

    Miguel Dias Moura, Oct 15, 2004, in forum: ASP .Net
    Replies:
    2
    Views:
    305
    Cirrosi
    Oct 16, 2004
  2. Shapper

    Increase value by 1. How to do this?

    Shapper, Jul 13, 2005, in forum: ASP .Net
    Replies:
    3
    Views:
    463
    Richard Myers
    Jul 13, 2005
  3. rp
    Replies:
    1
    Views:
    539
    red floyd
    Nov 10, 2011
  4. Une bévue
    Replies:
    5
    Views:
    153
    Une bévue
    Aug 10, 2006
  5. Srijayanth Sridhar
    Replies:
    19
    Views:
    627
    David A. Black
    Jul 2, 2008
Loading...

Share This Page