Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #37422 > unrolled thread
| Started by | moonhkt <moonhkt@gmail.com> |
|---|---|
| First post | 2013-01-22 23:26 -0800 |
| Last post | 2013-01-24 19:30 +0100 |
| Articles | 14 — 8 participants |
Back to article view | Back to comp.lang.python
Increase value in hash table moonhkt <moonhkt@gmail.com> - 2013-01-22 23:26 -0800
Re: Increase value in hash table Chris Rebert <clp2@rebertia.com> - 2013-01-22 23:34 -0800
Re: Increase value in hash table moonhkt <moonhkt@gmail.com> - 2013-01-23 01:23 -0800
Re: Increase value in hash table Oscar Benjamin <oscar.j.benjamin@gmail.com> - 2013-01-23 10:12 +0000
Re: Increase value in hash table Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2013-01-23 10:47 +0000
Multiple postings [was Re: Increase value in hash table] Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2013-01-23 21:54 +1100
Re: Multiple postings [was Re: Increase value in hash table] rusi <rustompmody@gmail.com> - 2013-01-23 06:00 -0800
Re: Increase value in hash table Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2013-01-23 10:46 +0000
Re: Increase value in hash table Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2013-01-23 10:46 +0000
Re: Increase value in hash table Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2013-01-23 10:47 +0000
Re: Increase value in hash table moonhk <moonhkt@gmail.com> - 2013-01-23 23:33 +0800
Re: Increase value in hash table moonhkt <moonhkt@gmail.com> - 2013-01-23 07:39 -0800
Re: Increase value in hash table Dave Angel <d@davea.name> - 2013-01-23 17:35 -0500
Re: Increase value in hash table Vito De Tullio <vito.detullio@gmail.com> - 2013-01-24 19:30 +0100
| From | moonhkt <moonhkt@gmail.com> |
|---|---|
| Date | 2013-01-22 23:26 -0800 |
| Subject | Increase value in hash table |
| Message-ID | <32d316a1-8551-4cb2-871f-b0a653c7d2f6@u7g2000yqg.googlegroups.com> |
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'
[toc] | [next] | [standalone]
| From | Chris Rebert <clp2@rebertia.com> |
|---|---|
| Date | 2013-01-22 23:34 -0800 |
| Message-ID | <mailman.870.1358926498.2939.python-list@python.org> |
| In reply to | #37422 |
[Multipart message — attachments visible in raw view] — view raw
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/collections.html#collections.Counter
[toc] | [prev] | [next] | [standalone]
| From | moonhkt <moonhkt@gmail.com> |
|---|---|
| Date | 2013-01-23 01:23 -0800 |
| Message-ID | <a9666eea-3143-4c72-87cb-8b8ec4945f28@z8g2000yqo.googlegroups.com> |
| In reply to | #37423 |
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/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'
[toc] | [prev] | [next] | [standalone]
| From | Oscar Benjamin <oscar.j.benjamin@gmail.com> |
|---|---|
| Date | 2013-01-23 10:12 +0000 |
| Message-ID | <mailman.881.1358935947.2939.python-list@python.org> |
| In reply to | #37422 |
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
[toc] | [prev] | [next] | [standalone]
| From | Steven D'Aprano <steve+comp.lang.python@pearwood.info> |
|---|---|
| Date | 2013-01-23 10:47 +0000 |
| Message-ID | <50ffbfd7$0$30003$c3e8da3$5496439d@news.astraweb.com> |
| In reply to | #37440 |
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
[toc] | [prev] | [next] | [standalone]
| From | Steven D'Aprano <steve+comp.lang.python@pearwood.info> |
|---|---|
| Date | 2013-01-23 21:54 +1100 |
| Subject | Multiple postings [was Re: Increase value in hash table] |
| Message-ID | <50ffc167$0$29985$c3e8da3$5496439d@news.astraweb.com> |
| In reply to | #37443 |
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
[toc] | [prev] | [next] | [standalone]
| From | rusi <rustompmody@gmail.com> |
|---|---|
| Date | 2013-01-23 06:00 -0800 |
| Subject | Re: Multiple postings [was Re: Increase value in hash table] |
| Message-ID | <34cd32ca-9e36-40a7-ba12-7801e04c7bab@t6g2000pba.googlegroups.com> |
| In reply to | #37449 |
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!!
[toc] | [prev] | [next] | [standalone]
| From | Steven D'Aprano <steve+comp.lang.python@pearwood.info> |
|---|---|
| Date | 2013-01-23 10:46 +0000 |
| Message-ID | <50ffbf9a$0$29995$c3e8da3$5496439d@news.astraweb.com> |
| In reply to | #37440 |
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
[toc] | [prev] | [next] | [standalone]
| From | Steven D'Aprano <steve+comp.lang.python@pearwood.info> |
|---|---|
| Date | 2013-01-23 10:46 +0000 |
| Message-ID | <50ffbf7c$0$29994$c3e8da3$5496439d@news.astraweb.com> |
| In reply to | #37440 |
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
[toc] | [prev] | [next] | [standalone]
| From | Steven D'Aprano <steve+comp.lang.python@pearwood.info> |
|---|---|
| Date | 2013-01-23 10:47 +0000 |
| Message-ID | <50ffbfb8$0$29966$c3e8da3$5496439d@news.astraweb.com> |
| In reply to | #37440 |
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
[toc] | [prev] | [next] | [standalone]
| From | moonhk <moonhkt@gmail.com> |
|---|---|
| Date | 2013-01-23 23:33 +0800 |
| Message-ID | <mailman.903.1358955191.2939.python-list@python.org> |
| In reply to | #37422 |
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
<oscar.j.benjamin@gmail.com> wrote:
> 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
--
moonhkt
GMT+8
[toc] | [prev] | [next] | [standalone]
| From | moonhkt <moonhkt@gmail.com> |
|---|---|
| Date | 2013-01-23 07:39 -0800 |
| Message-ID | <3ea1fef0-4a59-49f6-9e4f-ee62009bbb8e@y3g2000pbq.googlegroups.com> |
| In reply to | #37482 |
On Jan 23, 11:33 pm, moonhk <moon...@gmail.com> 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
>
>
>
>
>
>
>
>
>
> <oscar.j.benja...@gmail.com> wrote:
> > On 23 January 2013 07:26, 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
>
> >> # 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
[toc] | [prev] | [next] | [standalone]
| From | Dave Angel <d@davea.name> |
|---|---|
| Date | 2013-01-23 17:35 -0500 |
| Message-ID | <mailman.925.1358980542.2939.python-list@python.org> |
| In reply to | #37484 |
On 01/23/2013 10:39 AM, moonhkt wrote:
> On Jan 23, 11:33 pm, moonhk <moon...@gmail.com> 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
[toc] | [prev] | [next] | [standalone]
| From | Vito De Tullio <vito.detullio@gmail.com> |
|---|---|
| Date | 2013-01-24 19:30 +0100 |
| Message-ID | <mailman.1006.1359052235.2939.python-list@python.org> |
| In reply to | #37422 |
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
[toc] | [prev] | [standalone]
Back to top | Article view | comp.lang.python
csiph-web