เมื่อจำเป็นต้องสาธิตการแยกตัวนับและพจนานุกรม สามารถใช้ตัวนับและพจนานุกรมได้
ด้านล่างนี้เป็นการสาธิตสิ่งเดียวกัน -
ตัวอย่าง
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' ที่แสดงบนคอนโซล