how to print only unique words in a class string
Question:
this is how I obtained the data input at the beginning:
with open("wordslist.txt") as f:
words_list = {word.removesuffix("n") for word in f}
with open("negation_handling.csv") as g:
for tweete in g:
for word in tweete.split():
if word not in words_list:
print(word)
This code resulting in a data with the type of <class 'str'>
. this class string contains a lot of words that have duplicates. I wanted to print all of the words, but no words are repeated (delete all the duplicates). here is how the class looks, the class name is word
:
gfg
best
gfg
I
am
I
two
two
three
..............
my list of strings contains around 4500 words, and it is separated by a newline (enter) just like in the example of my question. now I cannot copy paste the strings because they are too many, so I store it in a class called "word" but I don’t know how to call the class. I wanted the code to do a looping and remove all the duplicate words so the output would be like this:
gfg best I am two three..........
this is what I tried:
input_list_of_strings = word
# Create empty list to store unique
unique_words = []
# Loop through each word and check if it exists in unique words list
for word in input_list_of_strings:
if word not in unique_words:
unique_words.append(word)
# Print the result
print(unique_words)
but the results are like this:
['e']
how can I call the class word
correctly?
Answers:
- If your input is a list of strings you could simply remember all unique strings using loop:
input_list_of_strings = ['gfg', 'best', 'gfg', 'I', 'am', 'I', 'two', 'two', 'three']
# Create empty list to store unique
unique_words = []
# Loop through each word and check if it exists in unique words list
for word in input_list_of_strings:
if word not in unique_words:
unique_words.append(word)
# Print the result
print(unique_words)
- Also, you could use python set, but pay attention that it won’t save the initial word order
input_list_of_strings = ['gfg', 'best', 'gfg', 'I', 'am', 'I', 'two', 'two', 'three']
# Create a set of unique words from the list
unique_words = set(input_list_of_strings)
unique_words_list = list(unique_words)
# Print the result
print(unique_words_list)
Hope it helps =)
this is how I obtained the data input at the beginning:
with open("wordslist.txt") as f:
words_list = {word.removesuffix("n") for word in f}
with open("negation_handling.csv") as g:
for tweete in g:
for word in tweete.split():
if word not in words_list:
print(word)
This code resulting in a data with the type of <class 'str'>
. this class string contains a lot of words that have duplicates. I wanted to print all of the words, but no words are repeated (delete all the duplicates). here is how the class looks, the class name is word
:
gfg
best
gfg
I
am
I
two
two
three
..............
my list of strings contains around 4500 words, and it is separated by a newline (enter) just like in the example of my question. now I cannot copy paste the strings because they are too many, so I store it in a class called "word" but I don’t know how to call the class. I wanted the code to do a looping and remove all the duplicate words so the output would be like this:
gfg best I am two three..........
this is what I tried:
input_list_of_strings = word
# Create empty list to store unique
unique_words = []
# Loop through each word and check if it exists in unique words list
for word in input_list_of_strings:
if word not in unique_words:
unique_words.append(word)
# Print the result
print(unique_words)
but the results are like this:
['e']
how can I call the class word
correctly?
- If your input is a list of strings you could simply remember all unique strings using loop:
input_list_of_strings = ['gfg', 'best', 'gfg', 'I', 'am', 'I', 'two', 'two', 'three']
# Create empty list to store unique
unique_words = []
# Loop through each word and check if it exists in unique words list
for word in input_list_of_strings:
if word not in unique_words:
unique_words.append(word)
# Print the result
print(unique_words)
- Also, you could use python set, but pay attention that it won’t save the initial word order
input_list_of_strings = ['gfg', 'best', 'gfg', 'I', 'am', 'I', 'two', 'two', 'three']
# Create a set of unique words from the list
unique_words = set(input_list_of_strings)
unique_words_list = list(unique_words)
# Print the result
print(unique_words_list)
Hope it helps =)