Trying to get unique words from a set of strings - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: General Coding Help (https://python-forum.io/forum-8.html) +--- Thread: Trying to get unique words from a set of strings (/thread-25909.html) |
Trying to get unique words from a set of strings - garam0 - Apr-15-2020 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? [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] RE: Trying to get unique words from a set of strings - bowlofred - Apr-15-2020 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) RE: Trying to get unique words from a set of strings - deanhystad - Apr-15-2020 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) RE: Trying to get unique words from a set of strings - menator01 - Apr-15-2020 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}')
RE: Trying to get unique words from a set of strings - ndc85430 - Apr-15-2020 Do you actually care about the order? If not, just use a set instead of a list. RE: Trying to get unique words from a set of strings - garam0 - Apr-15-2020 thank you all for your help! |