-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy path10.9.py
More file actions
42 lines (38 loc) · 1.05 KB
/
10.9.py
File metadata and controls
42 lines (38 loc) · 1.05 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
#coding:utf-8
from random import *
import time
"""
写一个函数,读取文件 words.txt,然后建立一个列表,这个列表中每个元素就是这个文件中
的每个单词。写两个版本的这样的函数,一个使用 append 方法,另外一个用自增运算的模
式:t= t + [x]。看看哪个运行时间更长?为什么会这样?
"""
fin = open('C:\Users\LzyRapx\PycharmProjects\untitled\words.txt')
def create_list(doc):
l =[]
while True:
line = doc.readline()
l.append(line)
if not line:
break
return l
def create_dict(doc):
d = {}
while True:
line = doc.readline()
d[line.strip()] =True
if not line:
break
return d
def create_list2(doc):
l = []
while True:
line = doc.readline()
l = l + [line]
if not line:
break
return l
if __name__=='__main__':
start = time.clock()
print create_dict(fin)
end = time.clock()
print (end-start),'s'