Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Python counter and dictionary intersection example
When working with string manipulation and character frequency analysis, the Counter class from the collections module provides an elegant way to count character occurrences. The intersection operation (&) between two Counter objects helps determine if one string's characters are a subset of another's.
Understanding Counter Intersection
The intersection of two Counter objects returns the minimum count of each common element. This is useful for checking if one string can be formed using characters from another string ?
from collections import Counter
def can_form_string(str_1, str_2):
dict_one = Counter(str_1.lower())
dict_two = Counter(str_2.lower())
result = dict_one & dict_two
return result == dict_one
string_1 = 'Hi Mark'
string_2 = 'how are yoU'
print("The first string is:")
print(string_1)
print("The second string is:")
print(string_2)
if can_form_string(string_1, string_2):
print("It is possible to form string 1 using characters from string 2")
else:
print("It is not possible to form string 1 using characters from string 2")
The first string is: Hi Mark The second string is: how are yoU It is not possible to form string 1 using characters from string 2
How Counter Intersection Works
Let's examine the character counts to understand why the result is "not possible" ?
from collections import Counter
string_1 = 'Hi Mark'
string_2 = 'how are yoU'
counter_1 = Counter(string_1.lower())
counter_2 = Counter(string_2.lower())
print("Counter for string 1:", counter_1)
print("Counter for string 2:", counter_2)
print("Intersection:", counter_1 & counter_2)
print("Are they equal?", (counter_1 & counter_2) == counter_1)
Counter for string 1: Counter({'i': 1, ' ': 1, 'h': 1, 'm': 1, 'a': 1, 'r': 1, 'k': 1})
Counter for string 2: Counter({' ': 2, 'o': 2, 'a': 2, 'h': 1, 'w': 1, 'r': 1, 'e': 1, 'y': 1, 'u': 1})
Intersection: Counter({' ': 1, 'h': 1, 'a': 1, 'r': 1})
Are they equal? False
Working Example
Here's an example where string formation is possible ?
from collections import Counter
def can_form_string(str_1, str_2):
dict_one = Counter(str_1.lower())
dict_two = Counter(str_2.lower())
result = dict_one & dict_two
return result == dict_one
string_1 = 'are'
string_2 = 'how are you'
print("The first string is:", string_1)
print("The second string is:", string_2)
if can_form_string(string_1, string_2):
print("It is possible to form string 1 using characters from string 2")
else:
print("It is not possible to form string 1 using characters from string 2")
The first string is: are The second string is: how are you It is possible to form string 1 using characters from string 2
Key Points
Counter intersection (
&) returns minimum counts of common elementsConverting to lowercase ensures case-insensitive comparison
The method checks if all characters in string 1 are available in sufficient quantity in string 2
Spaces and special characters are also counted as valid characters
Conclusion
Counter intersection is a powerful tool for checking character availability between strings. Use this approach when you need to verify if one string can be formed using characters from another, considering character frequency.
