이 기사에서 우리는 아래 주어진 문제 설명에 대한 해결책에 대해 배울 것입니다.
문제 설명 − 두 개의 문자열이 주어졌으므로 주어진 문자열에서 흔하지 않은 단어를 가져와야 합니다.
이제 아래 구현에서 솔루션을 관찰해 보겠습니다 -
예
# 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 프로그램을 만드는 방법을 배웠습니다.