Python 程序来查找两个字符串中不常见的字词
在本文中,我们将了解如下内容的问题陈述的解决方案。
问题陈述 - 给定两个字符串,我们需要从此字符串中获取不常见的字词。
现在让我们观察如下实现中的解决方案 -
示例
# uncommon words
def find(A, B):
# count
count = {}
# insert in A
for word in A.split():
count[word] = count.get(word, 0) + 1
# insert in B
for word in B.split():
count[word] = count.get(word, 0) + 1
# return ans
return [word for word in count if count[word] == 1]
# main
A = "Tutorials point "
B = "Python on Tutorials point"
print("The uncommon words in strings are:",find(A, B))输出
The uncommon words in strings are: ['Python', 'on']

所有变量都在局部范围内声明,并且在上图中可见其引用的对象。
结论
在本文中,我们已经了解了我们如何编写 Python 程序来查找两个字符串中的不常见字词
广告
数据结构
网络
RDBMS
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
Javascript
PHP