파이썬에서 문자열에서 모든 중복을 제거하려면 먼저 문자열을 공백으로 분할하여 배열에 각 단어를 포함해야 합니다. 그런 다음 중복을 제거하는 여러 가지 방법이 있습니다.
먼저 모든 단어를 소문자로 변환한 다음 정렬하고 마지막으로 고유한 단어만 선택하여 중복을 제거할 수 있습니다. 예를 들어,
예
sent = "Hi my name is John Doe John Doe is my name"
# Seperate out each word
words = sent.split(" ")
# Convert all words to lowercase
words = map(lambda x:x.lower(), words)
# Sort the words in order
words.sort()
unique = []
total_words = len(words)
i = 0
while i < (total_words - 1):
while i < total_words and words[i] == words[i + 1]:
i += 1
unique.append(words[i])
i += 1
print(unique) 출력
이것은 출력을 줄 것입니다 -
['doe', 'hi', 'john', 'is', 'my']