Comparing values of counter in python 3.3

A

Amjad Syed

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
 
W

Wolfgang Maier

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
 
M

Mark Lawrence

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 :)
 
A

alex23

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

rusi

[(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.
 
C

Chris Angelico

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
 
R

Roy Smith

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 :)
 
R

rusi

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

Mark Lawrence

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? :)
 
D

Devin Jeanpierre

[(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
473,994
Messages
2,570,223
Members
46,812
Latest member
GracielaWa

Latest Threads

Top