Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #61675 > unrolled thread

Comparing values of counter in python 3.3

Started byAmjad Syed <amjadcsu@gmail.com>
First post2013-12-11 23:49 -0800
Last post2013-12-16 01:50 -0800
Articles 10 — 8 participants

Back to article view | Back to comp.lang.python


Contents

  Comparing values of counter in python 3.3 Amjad Syed <amjadcsu@gmail.com> - 2013-12-11 23:49 -0800
    Re: Comparing values of counter in python 3.3 Wolfgang Maier <xpysol@gmail.com> - 2013-12-12 01:55 -0800
      Re: Comparing values of counter in python 3.3 Mark Lawrence <breamoreboy@yahoo.co.uk> - 2013-12-12 10:11 +0000
    Re: Comparing values of counter in python 3.3 alex23 <wuwei23@gmail.com> - 2013-12-16 11:59 +1000
      Re: Comparing values of counter in python 3.3 rusi <rustompmody@gmail.com> - 2013-12-15 18:32 -0800
        Re: Comparing values of counter in python 3.3 Chris Angelico <rosuav@gmail.com> - 2013-12-16 13:37 +1100
        Re: Comparing values of counter in python 3.3 Roy Smith <roy@panix.com> - 2013-12-15 21:40 -0500
          Re: Comparing values of counter in python 3.3 rusi <rustompmody@gmail.com> - 2013-12-15 19:28 -0800
          Re: Comparing values of counter in python 3.3 Mark Lawrence <breamoreboy@yahoo.co.uk> - 2013-12-16 09:20 +0000
        Re: Comparing values of counter in python 3.3 Devin Jeanpierre <jeanpierreda@gmail.com> - 2013-12-16 01:50 -0800

#61675 — Comparing values of counter in python 3.3

FromAmjad Syed <amjadcsu@gmail.com>
Date2013-12-11 23:49 -0800
SubjectComparing values of counter in python 3.3
Message-ID<7b7028b8-5de0-4a3a-a23b-2e19fe859955@googlegroups.com>
Hello,

I have 2 counters generated from list using  Collections.counter()

I want to print only key,values in Counter2 which have values > then corresponding value in Counter1.

E.g
Counter1={97:1,99:2,196:2,198:1}
Counter2={97:1 ,99:3, 196:1,198:1}

# Output
[99,3]
# Need to compare values of counter and reject in function/routine in value in counter2 is higher then value in counter1 for a current key


Thanks

[toc] | [next] | [standalone]


#61691

FromWolfgang Maier <xpysol@gmail.com>
Date2013-12-12 01:55 -0800
Message-ID<f7f14483-99e0-47c9-adaa-9392f858a6a9@googlegroups.com>
In reply to#61675
> I want to print only key,values in Counter2 which have values > then corresponding value in Counter1.
> E.g
> Counter1={97:1,99:2,196:2,198:1}
> Counter2={97:1 ,99:3, 196:1,198:1}
> 
> # Output
> [99,3]
>

Try:

[[key, Counter2[key]] for key in Counter1 if Counter2[key] > Counter1[key]]

for a start.
If you can't guarantee that every key from Counter1 is also in Counter2 you could use something like:

[[key, Counter2[key]] for key in Counter1 if key in Counter2 and Counter2[key] > Counter1[key]]

Best,
Wolfgang

[toc] | [prev] | [next] | [standalone]


#61692

FromMark Lawrence <breamoreboy@yahoo.co.uk>
Date2013-12-12 10:11 +0000
Message-ID<mailman.3978.1386843106.18130.python-list@python.org>
In reply to#61691
On 12/12/2013 09:55, Wolfgang Maier wrote:
>> I want to print only key,values in Counter2 which have values > then corresponding value in Counter1.
>> E.g
>> Counter1={97:1,99:2,196:2,198:1}
>> Counter2={97:1 ,99:3, 196:1,198:1}
>>
>> # Output
>> [99,3]
>>
>
> Try:
>
> [[key, Counter2[key]] for key in Counter1 if Counter2[key] > Counter1[key]]
>
> for a start.
> If you can't guarantee that every key from Counter1 is also in Counter2 you could use something like:
>
> [[key, Counter2[key]] for key in Counter1 if key in Counter2 and Counter2[key] > Counter1[key]]
>
> Best,
> Wolfgang
>

Personal preference I suppose, but give me a for loop any day of the 
week, guess I just find them more readable :)

-- 
My fellow Pythonistas, ask not what our language can do for you, ask 
what you can do for our language.

Mark Lawrence

[toc] | [prev] | [next] | [standalone]


#61980

Fromalex23 <wuwei23@gmail.com>
Date2013-12-16 11:59 +1000
Message-ID<l8lmq6$6ps$1@dont-email.me>
In reply to#61675
On 12/12/2013 5:49 PM, Amjad Syed wrote:
> Hello,
>
> I have 2 counters generated from list using  Collections.counter()
>
> I want to print only key,values in Counter2 which have values > then corresponding value in Counter1.
>
> E.g
> Counter1={97:1,99:2,196:2,198:1}
> Counter2={97:1 ,99:3, 196:1,198:1}
>
> # Output
> [99,3]
> # Need to compare values of counter and reject in function/routine in value in counter2 is higher then value in counter1 for a current key

     [(k,Counter2[k]) for k in Counter2 - Counter1]

Counters are awesome.

[toc] | [prev] | [next] | [standalone]


#61981

Fromrusi <rustompmody@gmail.com>
Date2013-12-15 18:32 -0800
Message-ID<905d6e7e-6748-42dd-8b63-d80a4d175914@googlegroups.com>
In reply to#61980
On Monday, December 16, 2013 7:29:31 AM UTC+5:30, alex23 wrote:
> > # Need to compare values of counter and reject in function/routine in value in counter2 is higher then value in counter1 for a current key

>      [(k,Counter2[k]) for k in Counter2 - Counter1]

Why not just?

Counter2 - Counter1

And if you want to uncounterify it then
dict(Counter2 - Counter1)

> Counters are awesome.

Yes -- agreed. But 'counter' is a strange name -- after checking whether
'bag' and 'multiset' are there in the library, I would not think to
check anything else.

[toc] | [prev] | [next] | [standalone]


#61983

FromChris Angelico <rosuav@gmail.com>
Date2013-12-16 13:37 +1100
Message-ID<mailman.4164.1387161457.18130.python-list@python.org>
In reply to#61981
On Mon, Dec 16, 2013 at 1:32 PM, rusi <rustompmody@gmail.com> wrote:
> But 'counter' is a strange name -- after checking whether
> 'bag' and 'multiset' are there in the library, I would not think to
> check anything else.

Which is why we have this list. Question: Is there a way to do x, y,
and z, in Python? Answer: Check out this module! *returns time machine
keys to Guido*

ChrisA

[toc] | [prev] | [next] | [standalone]


#61984

FromRoy Smith <roy@panix.com>
Date2013-12-15 21:40 -0500
Message-ID<roy-46969D.21405715122013@news.panix.com>
In reply to#61981
In article <905d6e7e-6748-42dd-8b63-d80a4d175914@googlegroups.com>,
 rusi <rustompmody@gmail.com> wrote:

> On Monday, December 16, 2013 7:29:31 AM UTC+5:30, alex23 wrote:
> > > # Need to compare values of counter and reject in function/routine in 
> > > value in counter2 is higher then value in counter1 for a current key
> 
> >      [(k,Counter2[k]) for k in Counter2 - Counter1]
> 
> Why not just?
> 
> Counter2 - Counter1
> 
> And if you want to uncounterify it then
> dict(Counter2 - Counter1)
> 
> > Counters are awesome.
> 
> Yes -- agreed. But 'counter' is a strange name -- after checking whether
> 'bag' and 'multiset' are there in the library, I would not think to
> check anything else.

Bag and multiset are names only CS weenies would think to look for.  
Counter is the name for the rest of us :-)

[toc] | [prev] | [next] | [standalone]


#61989

Fromrusi <rustompmody@gmail.com>
Date2013-12-15 19:28 -0800
Message-ID<0e55d4ed-f8e0-4c72-b802-db05e43696e7@googlegroups.com>
In reply to#61984
On Monday, December 16, 2013 8:10:57 AM UTC+5:30, Roy Smith wrote:
>  rusi  wrote:

> > On Monday, December 16, 2013 7:29:31 AM UTC+5:30, alex23 wrote:
> > > > # Need to compare values of counter and reject in function/routine in 
> > > > value in counter2 is higher then value in counter1 for a current key
> > >      [(k,Counter2[k]) for k in Counter2 - Counter1]
> > Why not just?
> > Counter2 - Counter1
> > And if you want to uncounterify it then
> > dict(Counter2 - Counter1)
> > > Counters are awesome.
> > Yes -- agreed. But 'counter' is a strange name -- after checking whether
> > 'bag' and 'multiset' are there in the library, I would not think to
> > check anything else.

> Bag and multiset are names only CS weenies would think to look for.  
> Counter is the name for the rest of us :-)

Weenie? WEENIE?!? Harrumph! Im offended!! <wink>

More seriously: One issue with all the new good stuff --
comprehensions, generator expressions, dict comprehensions etc is that
the motivations/explanations are all couched imperatively.

So
1.

[something(v) for v in lst]

is understood as
2.

newlst = []
for v in lst:
  newlst.append(something(v))

and so the comprehension is just a one-liner for the for-loop
[Witness Mark's comments earlier that the for loop is more readable]

If instead the comprenension is seen as a programmers equivalent of the
set theory expression

3.

{something(v) | v ∈ lst }  # if the unicode gods deign to allow!

then they would be more easily appreciated and used.

The extreme example of this is that the implementation of
comprehensions were and continue to be wrong because of variable
leakage because 1 is equivalenced to 2 rather than 3.

To come back to the topic: Counter suggesting DO-ing counting
whereas  bag/multiset suggests BE-ing with counts;
ie the one sounds imperative, the other declarative

(which of course may mean its a CS-weenie speaking!)

[toc] | [prev] | [next] | [standalone]


#62026

FromMark Lawrence <breamoreboy@yahoo.co.uk>
Date2013-12-16 09:20 +0000
Message-ID<mailman.4189.1387185660.18130.python-list@python.org>
In reply to#61984
On 16/12/2013 02:40, Roy Smith wrote:
> In article <905d6e7e-6748-42dd-8b63-d80a4d175914@googlegroups.com>,
>   rusi <rustompmody@gmail.com> wrote:
>
>> On Monday, December 16, 2013 7:29:31 AM UTC+5:30, alex23 wrote:
>>>> # Need to compare values of counter and reject in function/routine in
>>>> value in counter2 is higher then value in counter1 for a current key
>>
>>>       [(k,Counter2[k]) for k in Counter2 - Counter1]
>>
>> Why not just?
>>
>> Counter2 - Counter1
>>
>> And if you want to uncounterify it then
>> dict(Counter2 - Counter1)
>>
>>> Counters are awesome.
>>
>> Yes -- agreed. But 'counter' is a strange name -- after checking whether
>> 'bag' and 'multiset' are there in the library, I would not think to
>> check anything else.
>
> Bag and multiset are names only CS weenies would think to look for.
> Counter is the name for the rest of us :-)
>

Give me bag or multiset any day of the week, it's blatantly obvious what 
they do.  Counter, what the heck? :)

-- 
My fellow Pythonistas, ask not what our language can do for you, ask 
what you can do for our language.

Mark Lawrence

[toc] | [prev] | [next] | [standalone]


#62034

FromDevin Jeanpierre <jeanpierreda@gmail.com>
Date2013-12-16 01:50 -0800
Message-ID<mailman.4195.1387187483.18130.python-list@python.org>
In reply to#61981
On Sun, Dec 15, 2013 at 6:32 PM, rusi <rustompmody@gmail.com> wrote:
> On Monday, December 16, 2013 7:29:31 AM UTC+5:30, alex23 wrote:
>> > # Need to compare values of counter and reject in function/routine in value in counter2 is higher then value in counter1 for a current key
>
>>      [(k,Counter2[k]) for k in Counter2 - Counter1]
>
> Why not just?
>
> Counter2 - Counter1
>
> And if you want to uncounterify it then
> dict(Counter2 - Counter1)

Because you get different counts.

>>> c1 = Counter('ab')
>>> c2 = Counter('aab')
>>> c2 - c1
Counter({'a': 1})
>>> [(k, c2[k]) for k in c2 - c1]
[('a', 2)]

Counter subtraction is multiset subtraction, not set subtraction.

-- Devin

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web