Python 计数器和字典交集示例
当需要展示计数器和字典交集时,可以使用计数器和字典。
以下是演示 -
示例
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”,在控制台上显示相关输出。
广告
数据结构
网络
RDBMS
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
Javascript
PHP