Get only unique words from a sentence in Python
Question:
Let’s say I have a string that says “mango mango peach”. How can I print only the unique words in that string.
The desired output for the above string would be [peach] as a list
Thanks!!
Answers:
First – split you string with empty space delimiter (split() method), than use Counter or calculate frequencies by your own code.
seq = "mango mango peach".split()
[x for x in seq if x not in seq[seq.index(x)+1:]]
You can use a Counter
to find the number of occurrences of each word, then make a list of all words that appear only once.
from collections import Counter
phrase = "mango peach mango"
counts = Counter(phrase.split())
print([word for word, count in counts.items() if count == 1])
# ['peach']
Python has a built in method called count that would work very well here
text = "mango mango peach apple apple banana"
words = text.split()
for word in words:
if text.count(word) == 1:
print(word)
else:
pass
(xenial)vash@localhost:~/python/stack_overflow$ python3.7 mango.py
peach
banana
Using list comprehension you can do this
[print(word) for word in words if text.count(word) == 1]
Here is another solution using sets.
sentence = "mango mango peach"
# Convert the sentence into a list
words = sentence.split()
print(words)
# Convert the list into a set. Only unique words will be stored in the set
unique_words = set(words)
print(unique_words)
Let’s say I have a string that says “mango mango peach”. How can I print only the unique words in that string.
The desired output for the above string would be [peach] as a list
Thanks!!
First – split you string with empty space delimiter (split() method), than use Counter or calculate frequencies by your own code.
seq = "mango mango peach".split()
[x for x in seq if x not in seq[seq.index(x)+1:]]
You can use a Counter
to find the number of occurrences of each word, then make a list of all words that appear only once.
from collections import Counter
phrase = "mango peach mango"
counts = Counter(phrase.split())
print([word for word, count in counts.items() if count == 1])
# ['peach']
Python has a built in method called count that would work very well here
text = "mango mango peach apple apple banana"
words = text.split()
for word in words:
if text.count(word) == 1:
print(word)
else:
pass
(xenial)vash@localhost:~/python/stack_overflow$ python3.7 mango.py peach banana
Using list comprehension you can do this
[print(word) for word in words if text.count(word) == 1]
Here is another solution using sets.
sentence = "mango mango peach"
# Convert the sentence into a list
words = sentence.split()
print(words)
# Convert the list into a set. Only unique words will be stored in the set
unique_words = set(words)
print(unique_words)