Python Forum
Trying to get unique words from a set of strings
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Trying to get unique words from a set of strings
#1
when I execute the following code I get ['red', 'pink', 'red', 'white', 'blue']. I cannot understand why there are 2 'red' words. Can someone help me with this?

string ='red,red,white,white,pink,blue,red,white,blue'
wordlist = string.split(',')
for word in wordlist:
if wordlist.count(word) > 1:
wordlist.remove(word)
print(wordlist)

thank you

(Apr-15-2020, 02:46 PM)garam0 Wrote: when I execute the following code I get ['red', 'pink', 'red', 'white', 'blue']. I cannot understand why there are 2 'red' words. Can someone help me with this?

string ='red,red,white,white,pink,blue,red,white,blue'
wordlist = string.split(',')
for word in wordlist:
if wordlist.count(word) > 1:
wordlist.remove(word)
print(wordlist)

thank you
[python]string ='red,red,white,white,pink,blue,white,white,blue,blue,white'
wordlist = string.split(',')
print(wordlist)
i = 0
for word in wordlist:
    if wordlist.count(word) > 1:
        wordlist.remove(word)
print(wordlist)
[/python]
Reply
#2
You are attempting to iterate through the list and delete elements from it at the same time. This confuses the iterator. You could iterate over a copy of the list, or delete them through other methods.

string ='red,red,white,white,pink,blue,white,white,blue,blue,white'
wordlist = string.split(',')
print(wordlist)
i = 0
for word in list(wordlist):   # list() makes a new list object
    if wordlist.count(word) > 1:
        wordlist.remove(word)
print(wordlist)
Reply
#3
You should never remove items from the thing you are iterating. Instead build a new list that has the desired characteristics.
string ='red,red,white,white,pink,blue,white,white,blue,blue,white'
wordlist = string.split(',')
print(wordlist)

# Make a list only containing unique words
uniquewords = [word for word in wordlist if wordlist.count(word) == 1]
print('Unique colors', uniquewords)

# Make a list where each word only occurs once
colors = set(wordlist)
print('Colors', colors)
Reply
#4
Solution I came up with:

string ='red,red,white,white,pink,blue,white,white,blue,blue,white'
wordlist = string.split(',')
# print(wordlist)

for word in wordlist:
    while wordlist.count(word) > 1:
        wordlist.remove(word)
print(f'{wordlist}')
Output:
['red', 'pink', 'blue', 'white']
I welcome all feedback.
The only dumb question, is one that doesn't get asked.
My Github
How to post code using bbtags


Reply
#5
Do you actually care about the order? If not, just use a set instead of a list.
Reply
#6
thank you all for your help!
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Trying to understand strings and lists of strings Konstantin23 2 698 Aug-06-2023, 11:42 AM
Last Post: deanhystad
  Splitting strings in list of strings jesse68 3 1,702 Mar-02-2022, 05:15 PM
Last Post: DeaD_EyE
  Generate a string of words for multiple lists of words in txt files in order. AnicraftPlayz 2 2,758 Aug-11-2021, 03:45 PM
Last Post: jamesaarr
  Finding multiple strings between the two same strings Slither 1 2,480 Jun-05-2019, 09:02 PM
Last Post: Yoriz
  searching file for unique words Siylo 2 2,476 Nov-20-2018, 08:28 PM
Last Post: wavic
  Compare all words in input() to all words in file Trianne 1 2,716 Oct-05-2018, 06:27 PM
Last Post: ichabod801
  For loops, strings and printing words with even characters Drone4four 8 5,026 Oct-05-2018, 09:23 AM
Last Post: volcano63
  How to find unique and common words per line from a txt file? sweet_swiss 6 4,237 Aug-11-2018, 01:28 PM
Last Post: Gribouillis
  lists, strings, and byte strings Skaperen 2 4,181 Mar-02-2018, 02:12 AM
Last Post: Skaperen
  replace specific words in strings Sama 3 3,609 Jul-23-2017, 01:51 PM
Last Post: ichabod801

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020