Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Comparing two different python counter objects

I’m working on an algorithm in Python that would take user input and tell them what new letters they would need to add to a string to make it into a different string, and I’ve been playing around a lot with the dictionaries created by the Counter method.

I want to compare two different dictionaries that are counting letters from strings (like the objects returned from using the Counter tool from the collections module). We can call these dictionaries D1 and D2. I want there to be two resulting dictionaries (R1 and R2), the first being the shared letters between the two, and the second being the letters needed to make R1 into R2 (the letters that are in D2 but not in D1).

For example:

# assuming they’ve been converted from counter objects into regular 
dictionaries #

D1 = {‘A’: 2, ‘B’: 1, ‘C’: 4, ‘D’: 5}
D2 = {‘A’: 3, ‘B’: 4, ‘C’ : 4, ‘D’: 7}

# Some sort of comparison function executed here #

Result:

R1={‘A’: 2, ‘B’: 3, ‘C’: 4, ‘D’: 5} 
R2 = {‘A’: 1, ‘B’: 1, ‘C’: 0 , ‘D’: 2} 
like image 293
Nick Hart Avatar asked Sep 14 '18 01:09

Nick Hart


People also ask

What does counter () do in Python?

Counter is a subclass of dict that's specially designed for counting hashable objects in Python. It's a dictionary that stores objects as keys and counts as values. To count with Counter , you typically provide a sequence or iterable of hashable objects as an argument to the class's constructor.

How do you subtract a counter in Python?

Counter subtract() method is used to subtract element counts from another mapping (counter) or iterable. Inputs and outputs may be zero or negative. In the above code, using subtract() we get the output Counter({'a':4, 'b':1, 'c':1}) after subtracting 'abc' from the initial Counter containing elements 'abcabacaa'.


1 Answers

If by shared letters you mean the Counter intersection, you can use the & operator and the amount of letters needed to convert R1 into R2 can be seen as the difference:

from collections import Counter

D1 = Counter({'A': 2, 'B': 1, 'C': 4, 'D': 5})
D2 = Counter({'A': 3, 'B': 4, 'C': 4, 'D': 7})

R1 = D1 & D2

print(R1)  # intersection:  min(c[x], d[x])
print(D2 - D1)  # subtract (keeping only positive counts)

Output

Counter({'D': 5, 'C': 4, 'A': 2, 'B': 1})
Counter({'B': 3, 'D': 2, 'A': 1})

If you want to keep negative counts, you can do it like this:

from collections import Counter

D1 = Counter({'A': 2, 'B': 1, 'C': 4, 'D': 5, 'E': 5})
D2 = Counter({'A': 3, 'B': 4, 'C': 4, 'D': 7, 'E': 3})

R2 = Counter({key: D2.get(key, 0) - value for key, value in D1.items()})
print(R2)

Output

Counter({'B': 3, 'D': 2, 'A': 1, 'C': 0, 'E': -2})

In the above example 'E' : -2 because the count of E is 5 in D1 and 3 in D2. Note: All the examples are in Python 3.5.

like image 87
Dani Mesejo Avatar answered Oct 28 '22 06:10

Dani Mesejo