카운터와 사전의 교집합을 시연해야 하는 경우 카운터와 사전을 사용할 수 있습니다.
아래는 동일한 데모입니다 -
예시
from collections import Counter
def make_string(str_1,str_2):
dict_one = Counter(str_1)
dict_two = Counter(str_2)
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 (make_string(string_1,string_2)==True):
print("It is possible")
else:
print("It is not possible") 출력
The first string is : Hi Mark The second string is : how are yoU It is not possible
설명
-
필요한 패키지를 가져옵니다.
-
두 개의 문자열을 가져와 카운터로 변환하는 메서드가 정의되어 있습니다.
-
그런 다음 사전에 할당됩니다.
-
사전 외부에는 두 개의 문자열이 정의되어 있으며 이 두 문자열을 전달하여 메서드를 호출합니다.
-
함수가 'True' 또는 'False'를 반환하는지 여부에 따라 해당 출력이 콘솔에 표시됩니다.