user3404921
user3404921

Reputation: 1

Forming a List from a large string of words

With this code:

def createWordList(filename):
    file=open(filename,'r')
    s=file.read()
    file.close()
    L=s.split()
    return L

And this text file: http://www.cs.ucsb.edu/~buoni/cs8/labs/lab05/wordlist.txt

I am to return a list of all the words in the text file. But when the function is called a la:

createWordList('wordlist.txt') 

My computer (core-i5) takes about 5-10 minutes to perform the task and then ultimately freezes. It can return the string of individual words in about 2 seconds though.

Upvotes: 0

Views: 118

Answers (2)

Arvind
Arvind

Reputation: 2565

f=open('wordlist.txt','r+')
listofwords=[]
for line in f:
    listofwords.append(line)
print(listofwords)

I faced no problem. took exactly 0.049 sec to form the list(process program without print). printing will take lot of time.

Upvotes: 1

Trimax
Trimax

Reputation: 2473

The 'file' object is a iterable, and you can iterate it with a 'for' stament. This is more memory efficient. Read this: Methods of File Objects

So, try this code:

def createWordList(filename):
    myList = []
    input_file = open(filename, mode='r')
    for line in input_file:
        myList.append(line)
    return myList

How works:

  • First, create the empty list.
  • Open the input_file.
  • Iterates the file object line by line.
  • Append each line to the list.
  • Then, return the list.

It takes no more than 3 seconds in process the whole file.

Upvotes: 0

Related Questions