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 #
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: