Comparing values of counter in python 3.3

Amjad Syed

Usenet Archive
Joined
Nov 27, 2013
Messages
3
Reaction score
0
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
 

Wolfgang Maier

Usenet Archive
Joined
Jan 28, 2013
Messages
49
Reaction score
0
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
 

Mark Lawrence

Usenet Archive
Joined
Oct 10, 2004
Messages
1,364
Reaction score
0
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 :)
 

alex23

Usenet Archive
Joined
Jan 20, 2005
Messages
1,334
Reaction score
0
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.
 

rusi

Usenet Archive
Joined
Dec 9, 2010
Messages
809
Reaction score
0
[(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.
 

Chris Angelico

Usenet Archive
Joined
Mar 28, 2011
Messages
4,720
Reaction score
0
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
 

Roy Smith

Usenet Archive
Joined
Jan 7, 2001
Messages
2,478
Reaction score
0
rusi said:
[(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 :)
 

rusi

Usenet Archive
Joined
Dec 9, 2010
Messages
809
Reaction score
0
rusi 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!)
 

Mark Lawrence

Usenet Archive
Joined
Oct 10, 2004
Messages
1,364
Reaction score
0
rusi said:
# 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? :)
 

Devin Jeanpierre

Usenet Archive
Joined
May 4, 2011
Messages
210
Reaction score
0
[(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
 

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

No members online now.

Forum statistics

Threads
548,049
Messages
3,104,246
Members
248,133
Latest member
DujonT

Latest Threads

Top