Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Creating a word list
#1
I would like to take any text document and make a word list from the file with no duplicates. I don't know much about programming. This is the best I have been able to come up with. Thanks!

 	
def MakeWordList():
    with open('test.txt','r') as f:
        data = f.read()
    return set([word for word in data.split()])

    print ()
Reply
#2
Something you may need to look into.
In:
Output:
hello this is test. Hello doing a test here.
Out:
Output:
{'doing', 'this', 'Hello', 'here.', 'a', 'test.', 'test', 'is', 'hello'}
Are Hello hello and test test. not duplicates?
Reply
#3
#Assuming below is the input in the text file

Hi Good Morning! How are you? I am fine. How are studying?
I am Apple. I am doing fine. Thanks for asking.
I am April. I am ok. Not good

raw_data = open(r"C:\Users\***\Desktop\Python Forum\word_list.txt")
for line in raw_data:
    unique_line_list = set(line.split())
    print(unique_line_list)
Output:
{'How', 'Hi', 'Morning!', 'are', 'am', 'fine.', 'I', 'you?', 'studying?', 'Good'} {'Apple.', 'for', 'doing', 'fine.', 'I', 'Thanks', 'asking.', 'am'} {'good', 'April.', 'Not', 'I', 'ok.', 'am'}
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  For Word, Count in List (Counts.Items()) new_coder_231013 6 2,500 Jul-21-2022, 02:51 PM
Last Post: new_coder_231013
  find some word in text list file and a bit change to them RolanRoll 3 1,482 Jun-27-2022, 01:36 AM
Last Post: RolanRoll
  Class-Aggregation and creating a list/dictionary IoannisDem 1 1,883 Oct-03-2021, 05:16 PM
Last Post: Yoriz
Question Problem: Check if a list contains a word and then continue with the next word Mangono 2 2,455 Aug-12-2021, 04:25 PM
Last Post: palladium
  Trying to get the first letter of every word in a list DanielCook 2 2,104 Jan-05-2021, 05:06 PM
Last Post: deanhystad
  Creating list of lists from generator object t4keheart 1 2,162 Nov-13-2020, 04:59 AM
Last Post: perfringo
  Creating a dictionary from a list Inkanus 5 3,109 Nov-06-2020, 06:11 PM
Last Post: DeaD_EyE
  Creating a list of dictionaries while iterating pythonnewbie138 6 3,196 Sep-27-2020, 08:23 PM
Last Post: pythonnewbie138
  Trying to find first 2 letter word in a list of words Oldman45 7 3,635 Aug-11-2020, 08:59 AM
Last Post: Oldman45
  Creating a list of RSSI value and send it to the server. Azuan 0 2,616 Jun-08-2020, 11:22 PM
Last Post: Azuan

Forum Jump:

User Panel Messages

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