[英]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 以及此文本文件: 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:
但是当函数被称为la时:
createWordList('wordlist.txt')
My computer (core-i5) takes about 5-10 minutes to perform the task and then ultimately freezes. 我的计算机(core-i5)大约需要5-10分钟执行任务,然后最终冻结。 It can return the string of individual words in about 2 seconds though.
它可以在大约2秒钟内返回单个单词的字符串。
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).
花费了0.049秒的时间形成了列表(没有打印的处理程序)。 printing will take lot of time.
打印将花费大量时间。
The 'file' object is a iterable, and you can iterate it with a 'for' stament. “文件”对象是可迭代的,您可以使用“ for”语句对其进行迭代。 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: 运作方式:
It takes no more than 3 seconds in process the whole file. 整个文件的处理时间不超过3秒。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.